#Coding=utf-8ImportSqlite3ImportOS#creating databases and CursorsifOs.path.exists ('test.db'): Conn=sqlite3.connect ('test.db') cur=conn.cursor ()Else: Conn=sqlite3.connect ('test.db') cur=conn.cursor ()#Create a tableCur.execute ('CREATE TABLE IF not EXISTS customer (ID varchar), NAME varchar,' 'SEX varchar, telephone varchar, PRIMARY KEY (ID))')Try: #Inserting Data forTinch[('1','Alex','Mans','189'),('2','Tom','Mans','139')]: Conn.execute ('INSERT into Customer VALUES (?,?,?,?)', T)#no error committed after commitConn.commit ()except: #error, rollbackConn.rollback ()#Close Cursorscur.close ()#Close Database LinkConn.close ()
Querying the database with cursors:
The cursor object has the following actions:
Execute ()--Execute SQL statement
executemany--executing multiple SQL statements
Close ()--close cursor
Fetchone ()--Takes a record from the result and points the cursor to the next record
Fetchmany ()--take multiple records from the results
Fetchall ()--Remove all records from the results
Scroll ()--cursor scrolling
1. Enquiry
Cur.execute ("SELECT * FROM Customer")
Cur. Fetchall ()
2. Modifications
Cur.execute ("Update customer set sex= ' women ' WHERE id = 1")
Cx.commit ()
3. Delete
Cur.execute ("Delete from customer where id = 1")
Conn.commit ()
4. Print in Chinese, print the string sequentially
For item in Cur.fetchall ():
For element in item:
Print element
Reference:
Http://www.cnblogs.com/yuxc/archive/2011/08/18/2143606.html
Http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001388320596292f925f46d56ef4c80a1c9d8e47e2d5711000
SQLite Read/write