python读写sqlite 3 数据库


#encoding:utf-8
import sqlite3
#打开数据库连接
conn = sqlite3.connect('test.db')
print("Opend database successfully")

##清除已存在的表 -students
##conn.execute('''DROP TABLE students''');
##conn.commit()

##创建一个表students
conn.execute('''CREATE TABLE IF NOT EXISTS students
		(ID INT PRIMARY KEY NOT NULL,
		NAME         TEXT  NOT NULL,
		AGE          INT   NOT NULL);''')
print("Table created successfully");

conn.commit()

##插入数据
conn.execute("INSERT INTO sTudents(ID,NAME,AGE)\
		VALUES(1,'Allen',25)");
conn.execute("INSERT INTO sTudents(ID,NAME,AGE)\
		VALUES(2,'Maxsu',20)");
conn.execute("INSERT INTO sTudents(ID,NAME,AGE)\
		VALUES(3,'Teddy',24)");

conn.commit()
print("Records Insert successfully");
print("--"*30);

##读取表students
cursor =conn.execute("SELECT * from students")
print ("ID NAME AGE")
for it in cursor:
	for i in range(len(it)):
		print(it[i],end=" ")
	print ('\n')
conn.close()