4、用程序执行SQL脚本
SQL 脚本:script.sql(UTF-8)
insert into user_tb values(null, '白龙马', '456789', 23);
create table buddha_tb(
_id integer primary key autoincrement,
name text,
pass text,
description);
create table fairy_tb(
_id integer primary key autoincrement,
fairy_name text,
fairy_pass text,
fairy_title);
Python 执行SQL脚本:
import sqlite3
conn = sqlite3.connect('test.db')
c = conn.cursor()
with open('script.sql', 'r', True, 'UTF-8') as f:
sql = f.read()
c.executescript(sql)
conn.commit()
c.close()
conn.close()
同理:
import sqlite3
conn = sqlite3.connect('test.db')
c = conn.cursor()
sql = '''
insert into user_tb values(null, '其他', '4567891', 24);
create table else_tb(
_id integer primary key autoincrement,
name text,
pass text,
description);
'''
c.executescript(sql)
conn.commit()
c.close()
conn.close()