通过Python从其他SQLite数据库写入SQLite数据库

2024-09-27 19:09:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个SQLite数据库。你知道吗

[sava@localhost 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
[sava@localhost 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.播放器和密码在注册过程中的早期写入。你知道吗


Tags: dbsqlitempconstatintenterdex
1条回答
网友
1楼 · 发布于 2024-09-27 19:09:24

INSERT语句向表中添加新记录。 如果已经有一条记录并执行了两次插入,那么之后就有三条记录。你知道吗

要更改现有记录中的值,请使用UPDATE语句。你知道吗

相关问题 更多 >

    热门问题