python如何使用sqlite3模块创建数据库表

'''
1.导入sqlite3模块
2.创建连接sqlite3.connect()
3.创建游标对象
4.编写创建表的sql的语句
5.执行sql(包括异常语句try)
6.关闭连接
'''

import sqlite3
#创建连接
con = sqlite3.connect('C:\python_learn\DBA\SQLite3demo\sqlite3demo.db')
print(con)
#创建游标对象
cur = con.cursor()
sql = '''create table t_person(
            pno INTEGER primary key autoincrement,
            pname VARCHAR not null,
            age integer 
            
            )'''

try:
    #执行sql
    cur.execute(sql)
    print("创建表成功")
except Exception as e:
    print(e)
    print("创建表失败")
finally:
    #关闭游标
    cur.close()
    #关闭连接
    con.close()
发布了14 篇原创文章 · 获赞 0 · 访问量 160

猜你喜欢

转载自blog.csdn.net/yimaoyingbi/article/details/104309841