sqlite python脚本
搞了一下sqlite的编程
1. 连接db
conn = sqlite3.connect("test.db")
2. 取游标
cursor = conn.cursor()
3. 做sql操作
这里重点说insert,要用? 做变量绑定,直接插变量是有问题的,会报错:no such column xxx 。
cursor.execute("@H_502_29@insert @H_502_29@into sms('time','number','msg') @H_502_29@values(?,?,? )",params)
4. 收尾工作
关游标,关连接
cursor.close()
conn.commit()
conn.close()
最后附一下测试的程序,
# coding=UTF-8
import sqlite3
#@H_502_29@insert @H_502_29@table sms (@H_502_29@time,@H_502_29@number,msg) @H_502_29@values() conn = sqlite3.@H_502_29@connect("test.db") @H_502_29@cursor = conn.@H_502_29@cursor() a1 = '2015-06-19 10:21:48 689610' a2 = '18221278606' a3 = u'【xx中心】OK: Free disk space is less than 20% on volume /opt:xml-lgj04' params=(a1,a2,a3) @H_502_29@cursor.@H_502_29@execute("insert into sms('time','number','msg') values(?,? )",params) @H_502_29@cursor.@H_502_29@close() conn.@H_502_29@commit() conn.@H_502_29@close()
另外注意一下utf的问题,文件首部要加#coding=utf-8。