-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.py
55 lines (45 loc) · 1.38 KB
/
sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/python
import informixdb
import os
Database = 'd_1460371357365469'
#Server='172.16.13.186'
Server='ifxserver1'
Username = 'tqbodnho'
Password = 'JSe2lR1cH6'
def test_SQL():
try:
cur = conn = None
#connect
conn = informixdb.connect(Database+'@'+Server,Username,Password)
if not conn:
raise Exception("Failed to connect via SQL to " + dbservername)
else:
print("connect !!!!")
#get cursor
cur = conn.cursor()
#query count of testtable(if not exists,created)
cur.execute("create table if not exists testtable(id int, str char(50))")
cur.execute("select count(*) from testtable")
a = cur.fetchall()
print("Count is :")
print(a[0][0])
#insert a record into testtable
cur.execute("insert into testtable values(1,'hello')")
print("inserted a record!")
#query count of testtable again
cur.execute("select count(*) from testtable")
a = cur.fetchall()
print("Count is :")
print(a[0][0])
#commit
conn.commit()
finally:
if cur:
cur.close()
if conn:
conn.close()
#main()
if __name__=='__main__':
print("-----------------------------------------this is a SQL test")
test_SQL()
print("-----------------------------------------this is a SQL test")