executemany

Any other way to import data files(like .csv) in python sqlite3 module ? [not insert one by one]

社会主义新天地 提交于 2019-12-03 14:43:21
问题 In sqlite3's client CLI, there is " .import file TABLE_name " to do it. But, I do not want to install sqlite3 to my server at present. In python sqlite3 module, we can creat and edit a DB. But, I have not found a way to import data-file to a TABLE, except inserting rows one by one. Any other way? 回答1: You could insert at one shot using executemany command instead of inserting one by one Lets say I have users.csv with following contents "Hugo","Boss" "Calvin","Klein" and basically open with

Any other way to import data files(like .csv) in python sqlite3 module ? [not insert one by one]

有些话、适合烂在心里 提交于 2019-12-03 04:43:26
In sqlite3's client CLI, there is " .import file TABLE_name " to do it. But, I do not want to install sqlite3 to my server at present. In python sqlite3 module, we can creat and edit a DB. But, I have not found a way to import data-file to a TABLE, except inserting rows one by one. Any other way? You could insert at one shot using executemany command instead of inserting one by one Lets say I have users.csv with following contents "Hugo","Boss" "Calvin","Klein" and basically open with csv module and pass it to .executemany function import csv,sqlite3 persons= csv.reader(open("users.csv")) con

MySqlDb throws Operand should contain 1 column(s) on insert ignore statement

半腔热情 提交于 2019-12-02 18:21:51
问题 While looking at some of the websocket methods that stack exchange offers, I wanted to save a few data points into a MySQL database. However, when I attempt to run an executemany command, I get the following error: _mysql_exceptions.OperationalError: (1241, 'Operand should contain 1 column(s)') While looking around SO, I found many examples of this error, but they have dealt with removing parenthesis on SELECT statements. I'm not using a SELECT . I'm attempting to INSERT . A short, contained,

For Loop or executemany - Python and SQLite3

冷暖自知 提交于 2019-12-01 16:20:58
问题 I have started to learn Python and SQL recently and have a question. Using Python with SQLite3 I have written the following code: # Use sqlite3 in the file import sqlite3 # Create people.db if it doesn't exist or connect to it if it does exist with sqlite3.connect("people.db") as connection: c = connection.cursor() # Create new table called people c.execute("""CREATE TABLE IF NOT EXISTS people(firstname TEXT, lastname TEXT, age INT, occupation TEXT)""") people_list = [ ('Simon', 'Doe', 20,

SQLite3 Python: executemany SELECT

陌路散爱 提交于 2019-11-30 20:49:48
I'm trying to get all the rows out of a table in one line with some WHERE constraints using the executemany function import sqlite3 con = sqlite3.connect('test.db') cur = con.cursor() cur.execute('CREATE TABLE IF NOT EXISTS Genre (id INTEGER PRIMARY KEY, genre TEXT NOT NULL)') values = [ (None, 'action'), (None, 'adventure'), (None, 'comedy'), ] cur.executemany('INSERT INTO Genre VALUES(?, ?)', values) ids=[1,2] cur.executemany('SELECT * FROM Genre WHERE id=?', ids) rows = cur.fetchall() print rows ERROR cur.executemany('SELECT * FROM Genre WHERE id=?', ids) sqlite3.ProgrammingError: You

SQLite3 Python: executemany SELECT

末鹿安然 提交于 2019-11-30 04:46:48
问题 I'm trying to get all the rows out of a table in one line with some WHERE constraints using the executemany function import sqlite3 con = sqlite3.connect('test.db') cur = con.cursor() cur.execute('CREATE TABLE IF NOT EXISTS Genre (id INTEGER PRIMARY KEY, genre TEXT NOT NULL)') values = [ (None, 'action'), (None, 'adventure'), (None, 'comedy'), ] cur.executemany('INSERT INTO Genre VALUES(?, ?)', values) ids=[1,2] cur.executemany('SELECT * FROM Genre WHERE id=?', ids) rows = cur.fetchall()

how to transform pandas dataframe for insertion via executemany() statement?

折月煮酒 提交于 2019-11-28 09:15:31
I have a fairly big pandas dataframe - 50 or so headers and a few hundred thousand rows of data - and I'm looking to transfer this data to a database using the ceODBC module. Previously I was using pyodbc and using a simple execute statement in a for loop but this was taking ridiculously long (1000 records per 10 minutes)... I'm now trying a new module and am trying to introduce executemany() although I'm not quite sure what's meant by sequence of parameters in: cursor.executemany("""insert into table.name(a, b, c, d, e, f) values(?, ?, ?, ?, ?), sequence_of_parameters) should it look like a

how to transform pandas dataframe for insertion via executemany() statement?

天涯浪子 提交于 2019-11-27 02:12:29
问题 I have a fairly big pandas dataframe - 50 or so headers and a few hundred thousand rows of data - and I'm looking to transfer this data to a database using the ceODBC module. Previously I was using pyodbc and using a simple execute statement in a for loop but this was taking ridiculously long (1000 records per 10 minutes)... I'm now trying a new module and am trying to introduce executemany() although I'm not quite sure what's meant by sequence of parameters in: cursor.executemany("""insert