0

There are many eaxmples of this, but in all cases that I saw they knew the names of the fields(columns). The two tables have exactly the same column/fields.

My solution has solved my current problem but as you can see from the code, it might qualify for the 'most ridiculous code of the year' award.

# 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()

There must be a better (more reliable) way to do this.

1 Answer 1

1

Use cursor.executemany:

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()

fields = ','.join('?' for desc in cursor.description)
stmt = "insert into {} values ({})".format(target_table, fields)
cursor.executemany(stmt, data)
connection.commit()
connection.close()

Used cursor.description to get column data.

NOTE

parameter marks vary according to database module. sqlite3 module use qmark(?). You should check it if you use another database module.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.