OSDN Git Service

【更新内容】
[ring-lang-081/ring.git] / docs / en / source / sqlite.txt
1 .. index:: 
2         single: SQLite; Introduction
3
4 ================
5 SQLite Functions
6 ================
7
8 In this chapter we will learn about using the SQLite database in the Ring programming language.
9
10 Before using the next function load the sqlitelib.ring library
11
12 .. code-block:: ring
13
14         load "sqlitelib.ring"
15         # Use SQLite functions
16
17 .. index:: 
18         pair: SQLite; sqlite_init()
19
20 sqlite_init() function
21 ======================
22
23 Syntax:
24
25 .. code-block:: ring
26
27         sqlite_init() ---> SQLite Object
28
29 .. index:: 
30         pair: SQLite; sqlite_open()
31
32 sqlite_open() function
33 ======================
34
35 Syntax:
36
37 .. code-block:: ring
38
39         sqlite_open(SQLite Object,cFileName)
40
41 .. index:: 
42         pair: SQLite; sqlite_execute()
43
44 sqlite_execute() function
45 =========================
46
47 Syntax:
48
49 .. code-block:: ring
50
51         sqlite_execute(SQLite Object,cSQLStatement)
52
53 .. index:: 
54         pair: SQLite; sqlite_close()
55
56 sqlite_close() function
57 =======================
58
59 Syntax:
60
61 .. code-block:: ring
62
63         sqlite_close(SQLite Object)
64
65 Example
66 =======
67
68 The next code create a SQLite database, add new records then display the data.
69
70 .. code-block:: ring
71
72         load "sqlitelib.ring"
73
74         oSQLite = sqlite_init()
75
76         sqlite_open(oSQLite,"mytest.db") 
77
78         sql = "
79                  CREATE TABLE COMPANY (
80                  ID INT PRIMARY KEY     NOT NULL,
81                  NAME           TEXT    NOT NULL,
82                  AGE            INT     NOT NULL,
83                  ADDRESS        CHAR(50),
84                  SALARY         REAL );
85         "
86         sqlite_execute(oSQLite,sql) 
87
88         sql = "
89                 INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) 
90                 VALUES  (1, 'Mahmoud' , 29, 'Jeddah', 20000.00 ),
91                         (2, 'Ahmed'   , 27, 'Jeddah', 15000.00 ),
92                         (3, 'Mohammed', 31, 'Egypt' , 20000.00 ),
93                         (4, 'Ibrahim' , 24, 'Egypt ', 65000.00 );
94         "
95
96         sqlite_execute(oSQLite,sql)
97
98         aResult =  sqlite_execute(oSQLite,"select * from COMPANY") 
99         for x in aResult
100                 for t in x
101                         ? t[2] + nl
102                 next
103         next
104         ? copy("*",50) 
105         for x in aResult
106                 ? x[:name] 
107         next
108         sqlite_close(oSQLite) 
109
110 Output:
111
112 .. code-block:: ring
113
114         1
115         Mahmoud
116         29
117         Jeddah
118         20000.0
119         2
120         Ahmed
121         27
122         Jeddah
123         15000.0
124         3
125         Mohammed
126         31
127         Egypt
128         20000.0
129         4
130         Ibrahim
131         24
132         Egypt
133         65000.0
134         **************************************************      
135         Mahmoud
136         Ahmed
137         Mohammed
138         Ibrahim
139