py + sqlite3 demo
实际问题
主要在编码问题下。
我把这解决的办法说下吧,主要这统一 编码 utf8
0. 数据库 导入 utf8
如果 导入文本 gbk
perl -MEncode -ne 'print encode("UTF-8", decode("GBK",$_));' file.txt > xx.data
1. 代码编码 # -*- coding:UTF8 -*-
2. 文件读取 编码 cc=bm(x.readline()).split('"t')
bm=lambda str : str.decode('gbk').encode('utf-8')
3. 对外输出 str="%s%s"t" % (str,bmug(f))
bmug=lambda str : unicode(str).encode("utf8")
就是统一编码 ,我这统一 utf8
#
-*- coding: UTF8 -*-
import sqlite3,os
"""
drop table imusic ;
create table imusic(
musicname varchar(300) ,
productid varchar(300) ,
downcount varchar(300) ,
musicname2 varchar(300) ,
actorname2 varchar(300) ,
iszl varchar(300) ,
isqu varchar(300) ,
zlmmf varchar(300) ,
zlmp3 varchar(300) ,
qump3 varchar(300) ,
cpid varchar(300) ,
cpname varchar(300) ,
rows varchar(300) ,
tmp1 varchar(300)
);
如果要使用主键
在建表第一列加 id integer auto_increment PRIMARY KEY
文本就要先处理下来
awk 'BEGIN{vi=0}{vi++;print vi"\t"$0;}' 03.data > 03.id.data
# 文本 "t 分割 导入
.separator "\t"
.import db2.data imusic
.separator ","
#导出
sqlite> .output a.txt
sqlite> select * from tab_xx;
sqlite> .output stdout
"""
conn = sqlite3.connect( " ex1 " )
cur = conn.cursor()
# 编码转换 (我这 在 windows 下)
bm = lambda str : str.decode( ' gbk ' ).encode( ' utf-8 ' )
bmcc = lambda str : str.decode( ' utf8 ' ).encode( ' gbk ' )
bmug = lambda str : unicode(str).encode( " utf8 " )
def getValues(actor,music,zlxz,qqxz):
try :
sql = " select * from imusic where musicname2=? and actorname2=? "
cur.execute(sql,( music,actor) )
res = cur.fetchall()
if res :
data = []
for tt in res[0]:
data.append(tt)
data[ 7 ] = ''
print zlxz, " * " ,qqxz
if data[ 8 ] == '' and data[ 9 ] == '' :
return None
if zlxz == ' 有振铃下载 ' :
data[ 8 ] = ''
if qqxz == ' 有全曲下载 ' :
data[ 9 ] = ''
if data[ 8 ] == '' and data[ 9 ] == '' :
return None
return data
except Exception, myError:
print " [error] %s %s %s %s " % (actor,music,zlxz,qqxz)
x = open( " all_comm.txt " , " r " )
cc = x.readline().split( ' \t ' )
while cc :
try :
cc = bm(x.readline()).split( ' \t ' )
dd = getValues(cc[ 6 ],cc[ 5 ],cc[ 21 ],cc[ 18 ])
if dd :
str = ""
for f in dd :
str = " %s%s\t " % (str,bmug(f))
print str
except Exception, myError:
print " [error] %s " % cc
import sqlite3,os
"""
drop table imusic ;
create table imusic(
musicname varchar(300) ,
productid varchar(300) ,
downcount varchar(300) ,
musicname2 varchar(300) ,
actorname2 varchar(300) ,
iszl varchar(300) ,
isqu varchar(300) ,
zlmmf varchar(300) ,
zlmp3 varchar(300) ,
qump3 varchar(300) ,
cpid varchar(300) ,
cpname varchar(300) ,
rows varchar(300) ,
tmp1 varchar(300)
);
如果要使用主键
在建表第一列加 id integer auto_increment PRIMARY KEY
文本就要先处理下来
awk 'BEGIN{vi=0}{vi++;print vi"\t"$0;}' 03.data > 03.id.data
# 文本 "t 分割 导入
.separator "\t"
.import db2.data imusic
.separator ","
#导出
sqlite> .output a.txt
sqlite> select * from tab_xx;
sqlite> .output stdout
"""
conn = sqlite3.connect( " ex1 " )
cur = conn.cursor()
# 编码转换 (我这 在 windows 下)
bm = lambda str : str.decode( ' gbk ' ).encode( ' utf-8 ' )
bmcc = lambda str : str.decode( ' utf8 ' ).encode( ' gbk ' )
bmug = lambda str : unicode(str).encode( " utf8 " )
def getValues(actor,music,zlxz,qqxz):
try :
sql = " select * from imusic where musicname2=? and actorname2=? "
cur.execute(sql,( music,actor) )
res = cur.fetchall()
if res :
data = []
for tt in res[0]:
data.append(tt)
data[ 7 ] = ''
print zlxz, " * " ,qqxz
if data[ 8 ] == '' and data[ 9 ] == '' :
return None
if zlxz == ' 有振铃下载 ' :
data[ 8 ] = ''
if qqxz == ' 有全曲下载 ' :
data[ 9 ] = ''
if data[ 8 ] == '' and data[ 9 ] == '' :
return None
return data
except Exception, myError:
print " [error] %s %s %s %s " % (actor,music,zlxz,qqxz)
x = open( " all_comm.txt " , " r " )
cc = x.readline().split( ' \t ' )
while cc :
try :
cc = bm(x.readline()).split( ' \t ' )
dd = getValues(cc[ 6 ],cc[ 5 ],cc[ 21 ],cc[ 18 ])
if dd :
str = ""
for f in dd :
str = " %s%s\t " % (str,bmug(f))
print str
except Exception, myError:
print " [error] %s " % cc
下面 demo 注重 update 对比
# conn.total_changes 使用
为 update 跟新
#
-*- coding: UTF8 -*-
import sqlite3,os,sys
conn = sqlite3.connect( " ex4 " )
cur = conn.cursor()
# 编码转换 (我这 在 windows 下)
bm = lambda str : str.decode( ' gbk ' ).encode( ' utf-8 ' )
bmcc = lambda str : str.decode( ' utf8 ' ).encode( ' gbk ' )
bmug = lambda str : unicode(str).encode( " utf8 " )
def getValues(actor,music,zlxz,qqxz,updateRow):
try :
updateSql = " update imusic set tmp1=? where musicname2=? and actorname2=? "
# 1 有振铃 有全曲
# 2 无振铃 有全曲
# 3 有振铃 无全曲
# 4 无振铃 无全曲
start = " 0 " ;
if zlxz == ' 有振铃下载 ' and qqxz == ' 有全曲下载 ' :
start = " 1 "
if zlxz == ' 无振铃下载 ' and qqxz == ' 有全曲下载 ' :
start = " 2 "
if zlxz == ' 有振铃下载 ' and qqxz == ' 无全曲下载 ' :
start = " 3 "
if zlxz == ' 无振铃下载 ' and qqxz == ' 无全曲下载 ' :
start = " 4 "
cur.execute(updateSql,(start,music,actor) )
if not conn.total_changes == updateRow:
updateRow = conn.total_changes
log = " [success=%s update=%s] %s-%s:%s %s " % (start,conn.total_changes,actor,music,zlxz,qqxz)
else :
log = " [giveUp start=%s] %s-%s:%s %s " % (start,actor,music,zlxz,qqxz)
print bmcc(log)
os.system( " echo %s >> log " % (bmcc(log)) )
conn.commit()
return updateRow
except Exception, myError:
excType, excValue, traceBack = sys.exc_info()
print excType
print myError
x = open( " all_comm2.txt " , " r " )
cc = x.readline().split( ' \t ' )
myrows = 0
updateRow = 0 ;
while cc :
myrows = myrows + 1
try :
print " rows = %s " % (myrows)
cc = bm(x.readline()).split( ' \t ' )
updateRow = getValues(cc[ 6 ],cc[ 5 ],cc[ 21 ],cc[ 18 ],updateRow)
except Exception, myError:
excType, excValue, traceBack = sys.exc_info()
print excType
print myError
import sqlite3,os,sys
conn = sqlite3.connect( " ex4 " )
cur = conn.cursor()
# 编码转换 (我这 在 windows 下)
bm = lambda str : str.decode( ' gbk ' ).encode( ' utf-8 ' )
bmcc = lambda str : str.decode( ' utf8 ' ).encode( ' gbk ' )
bmug = lambda str : unicode(str).encode( " utf8 " )
def getValues(actor,music,zlxz,qqxz,updateRow):
try :
updateSql = " update imusic set tmp1=? where musicname2=? and actorname2=? "
# 1 有振铃 有全曲
# 2 无振铃 有全曲
# 3 有振铃 无全曲
# 4 无振铃 无全曲
start = " 0 " ;
if zlxz == ' 有振铃下载 ' and qqxz == ' 有全曲下载 ' :
start = " 1 "
if zlxz == ' 无振铃下载 ' and qqxz == ' 有全曲下载 ' :
start = " 2 "
if zlxz == ' 有振铃下载 ' and qqxz == ' 无全曲下载 ' :
start = " 3 "
if zlxz == ' 无振铃下载 ' and qqxz == ' 无全曲下载 ' :
start = " 4 "
cur.execute(updateSql,(start,music,actor) )
if not conn.total_changes == updateRow:
updateRow = conn.total_changes
log = " [success=%s update=%s] %s-%s:%s %s " % (start,conn.total_changes,actor,music,zlxz,qqxz)
else :
log = " [giveUp start=%s] %s-%s:%s %s " % (start,actor,music,zlxz,qqxz)
print bmcc(log)
os.system( " echo %s >> log " % (bmcc(log)) )
conn.commit()
return updateRow
except Exception, myError:
excType, excValue, traceBack = sys.exc_info()
print excType
print myError
x = open( " all_comm2.txt " , " r " )
cc = x.readline().split( ' \t ' )
myrows = 0
updateRow = 0 ;
while cc :
myrows = myrows + 1
try :
print " rows = %s " % (myrows)
cc = bm(x.readline()).split( ' \t ' )
updateRow = getValues(cc[ 6 ],cc[ 5 ],cc[ 21 ],cc[ 18 ],updateRow)
except Exception, myError:
excType, excValue, traceBack = sys.exc_info()
print excType
print myError
整理 www.blogjava.net/Good-Game