2013-10-24 231 views
0

我有兩個SQLite數據庫。通過Python從其他SQLite數據庫寫入SQLite數據庫

[[email protected] python]$ sqlite3 herostat.db 
SQLite version 3.6.20       
Enter ".help" for instructions    
Enter SQL statements terminated with a ";" 
sqlite> .tables        
archer_stat warrior_stat wizard_stat  
sqlite> select * from warrior_stat; 
3000|300|9|9|5|1500|1700|700|200|120 
sqlite> .schema 
CREATE TABLE archer_stat 
    (con int, str int, wit int, _int int, dex int, mp int, pdef int, mdef int, patack int, matack int); 
CREATE TABLE warrior_stat 
    (con int, str int, wit int, _int int, dex int, mp int, pdef int, mdef int, patack int, matack int); 
CREATE TABLE wizard_stat 
    (con int, str int, wit int, _int int, dex int, mp int, pdef int, mdef int, patack int, matack int); 
sqlite> .exit 
[[email protected] python]$ sqlite3 players.db 
SQLite version 3.6.20 
Enter ".help" for instructions 
Enter SQL statements terminated with a ";" 
sqlite> .schema 
CREATE TABLE users 
    (login text, password text, _class text, con int, str int, wit int, _int int, dex int, mp int, pdef int, mdef int, patack int, matack int); 
sqlite> .exit 

我的Python 2.6代碼:

conn = db.connect('herostat.db') 
c = conn.cursor() 
c.execute("SELECT * FROM warrior_stat") 
cur_war = c.fetchone()[:] 

conn2 = db.connect('players.db') 
c2 = conn2.cursor() 
c2.execute("INSERT INTO users(_class) VALUES ('warrior')") 
c2.executemany('''INSERT INTO users(con, str, wit, _int, dex, mp, pdef, mdef, patack, matack) 
    VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', (cur_war,)) 
conn2.commit() 
c2.close() 

我想到:

player|password|warrior|3000|300|9|9|5|1500|1700|700|200|120 

我能得到什麼:

player|password||||||||||| 
||warrior|||||||||| 
|||3000|300|9|9|5|1500|1700|700|200|120 

我在做什麼錯=

P.S.玩家和密碼在流程註冊時儘早寫入。

+0

輸出從哪裏來? –

+0

我看到輸出到db players.db https://dl.dropboxusercontent.com/u/61798016/executemany.png –

+0

你用什麼命令來生成輸出? –

回答

0

INSERT語句向表中添加一條新記錄。 如果您已經有一條記錄並執行兩次INSERT,則之後有三條記錄。

要更改現有記錄中的值,請使用UPDATE語句。

+0

+1。謝謝。我正在嘗試使用更新創建查詢 –