日期:2014-05-16  浏览次数:20501 次

读文本文件到写入到数据库

早在去年年底的时候。爆出了好多明文密码,当时很猥琐的想把这些文本写到数据库中。

找开发,他们都懒得弄,今天我自己尝试用Python写了下,不过是SQL事务,数据大的时候 如果中途一个错误,需要重新导入

#!/usr/bin/env python
import  sqlite3
import  sys
import  os


if len(sys.argv)!=3:
    print "use:command filename DBname"
    sys.exit
        
else:
    DB_IMPORT_FILE=sys.argv[1]
    DB_SQLITE_PATH=sys.argv[2]

print 'command %s %s' %(DB_IMPORT_FILE,DB_SQLITE_PATH)

try:
    sqlite_conn = sqlite3.connect(DB_SQLITE_PATH)
except sqlite3.Error, e:
    print 'conntect sqlite database failed.'
    sqlite_logger.error("conntect sqlite database failed, ret = %s" % e.args[0])   
except:
    print "ERROR"
    
try:
    sqlite_conn.execute("create table if not exists PASS(id integer primary key autoincrement, name varchar(128), password varchar(128),email varchar(128))")      
except:
    print "sql error"

    


try:
    sqlite_cursor = sqlite_conn.cursor()
except:
    print "SQL Cur Error"

try:
    FILE=open(DB_IMPORT_FILE) 
except:
    print "cann.t open File"
    



for line in FILE.readlines():           
         list= line.split()
         sql="INSERT INTO PASS values('%s','%s','%s')" %(list[0],list[1],list[2])
         print sql
         print '============'
         sqlite_conn.execute(sql)
         sqlite_conn.commit()