0

これには多くの例がありますが、私が見たすべてのケースで、フィールド(列)の名前を知っていました。2 つのテーブルには、まったく同じ列/フィールドがあります。

私のソリューションは現在の問題を解決しましたが、コードからわかるように、「今年の最もばかげたコード」賞を受賞する資格があるかもしれません。

# Copy data from one table to another in the same database
print '-' * 70
print 'Copy data from one table to another in the same database\n'
print '  Usefull for creating test data.'
print '-' * 70

import sqlite3

connection = sqlite3.connect("table.sqlite")
cursor = connection.cursor()

source_table = 'table1'
target_table = 'test_table1'

stmt = "SELECT * FROM %s" % source_table

cursor.execute(stmt)
data = cursor.fetchall()

for row in data:
    stmt = "insert into %s values " % target_table + str(row)
    stmt = stmt.replace("u'", '"')
    stmt = stmt.replace("'", '"')
    stmt = stmt.replace(' None', ' Null')
    cursor.execute(stmt)
    connection.commit()

connection.close()

これを行うには、より良い (より信頼できる) 方法が必要です。

4

1 に答える 1