Import Sqlite3def Test1 (): con = sqlite3.connect ("d:\\test.db") con = sqlite3.connect ("Memory") cur =con.cursor () Try: Cur.execute (' CREATE TABLE score (ID integer primary key,name varchar (TEN), scores integer) ') Cur.execute ("INSERT INTO score VALUES (0, ' Rose ', ")") Cur.execute ("INSERT into score values (1, ' Alice ',") ") Cur.execute (" INSERT into score values (2, ' Helon ') Cur.execute ("INSERT INTO score values (3, ' Tom ', 98)") Cur.execute ("INSERT INTO score values (4, ' Jack ', 198)" #cur. Execute ("INSERT INTO score values (4, ' Jack ', 198)") #Don ' t execute the following statement when except Occurscur.exec Ute ("INSERT into score values (5, ' Tony ', 198)") Cur.execute ("INSERT into score values (6, ' David ',") ") Cur.execute (" Update score Set scores =? Where id=? ", (45,3)) #With Paramcur.execute (" Update score set name=? "). Where id=? ", (" John ", 0)) #With paramcur.execute (" Delete from score where ID =1 ") except Exception,e:print" There is some ex Cept ", Econ.commit () print" Insert complete "print"-----------------------------------------"Print" Last row ID was ", cur.lastrowidcur.execute (' select * from score ') print Cur.fetchall () print"----------------- -----------------------"Cur.execute (" SELECT count (*) from score ") print" Current Rows are: ", Cur.fetchall () [0]cur.close () Con.close () if __name__ = = ' __main__ ': Test1 () print "Hello World"
Python Learning Note Five----using the SQLite database