您的位置:首页 > 编程语言 > Python开发

python向数据库mysql,sqlite3插入外部变量

2018-01-31 21:10 489 查看
一,若要插入外部非字符串变量可以用

data = 55
cur.execute("insert into book(name,price) values('html',{})".format(data))
cur.commit()二,若要插入外部字符串变量时要用
bookname = 'Python'
cur.execute("insert into book(name,price) values('{}',55)".format(bookname))
cur.commit()三,对于mysql,若要插入中文字符要用语句
db = pymysql.connect(host = '127.0.0.1',user = 'root',passwd = '123456',db = 'test1',charset = 'utf8')四,创建mysql时若要使用中文字符时得使用
sql ="CREATE TABLE book(name varchar(20) NOT NULL,price integer )ENGINE=InnoDB DEFAULT CHARSET=utf8"

#执行sql语句
cursor.execute(sql)
db.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息