import sqlite3 #连接数据库 conn = sqlite3.connect("test.db") #打开或创建数据库文件(当不存在时会创建) print("Opened database successfully") c = conn.cursor() #获取游标 sql0 = '''drop table company;''' sql_create = ''' create table company (id int primary key not null, name text not null, age int not null, address char(50), salary real); ''' sql_insert_1 = ''' insert into company (id,name,age,address,salary) values(1,"monkey",32,"杭州",10000)''' sql_insert_2 = ''' insert into company (id,name,age,address,salary) values(2,"pig",32,"杭州",20000)''' sql_select= '''SELECT * FROM company;''' # c.execute(sql_create) #执行SQL语句 # conn.commit() #提交 # c.execute(sql_insert_1) # c.execute(sql_insert_2) # conn.commit() c.execute(sql_select) cursor = c.execute(sql_select) for row in cursor: print("id = ",row[0]) print("name = %s"%row[1]) print("address = ",row[2]) print("salary = ",row[3]," ") conn.close #关闭数据库 print("执行SQL成功")