SQLite3创建表_1

  • 使用sqlite3模块的connect方法来创建/打开数据库,需要指定数据库路径,不存在则创建一个新的数据库。

 1 '''
 2 1.导入sqlite3模块 
 3 2.创建连接 sqlite3.connect()
 4 3.创建游标对象
 5 4.编写创建表的sql语句
 6 5.执行sql
 7 6.关闭连接
 8 '''
 9 import sqlite3
10 #创建连接
11 con=sqlite3.connect('d:/sqlite3Demo/demo.db')
12 # 创建游标对象
13 cur=con.cursor()
14 #编写创建表的sql语句
15 sql='''create table t_person(
16           pno INTEGER primary key autoincrement,
17           pname VARCHAR not null,
18           age INTEGER 
19       )'''
20 try:
21     #执行sql语句
22     cur.execute(sql)
23     print('创建表成功')
24 except Exception as e:
25     print(e)
26     print('创建表失败')
27 finally:
28     #关闭游标
29     cur.close()
30     #关闭连接
31     con.close()

 1 创建表成功 

猜你喜欢

转载自www.cnblogs.com/monsterhy123/p/12575596.html