1

I have a set of data in a list, embedded into a dictionary as:

{'list1': ['Freddy','36','fred','123f','2017/04/25'], 'list2':['Tara','25','mtara','123t','2018/03/22']}

Ref notations:
{ key1: [name, age,nickname, userid, account_created_date],
..key2:[name, age,nickname, userid, account_created_date] }

All the data is inserted in variables in a Python function, one for each, as described above. When I call the function I would get the output right-away as

Output:
Freddy
Tara

But when I try to insert the data into a sqlite database, I get the output in the following manner:

Output:
F
R
E
D
D
Y
T
A
R
A

Code:
conn = sqlite3.connect(dbPath)
cur = conn.cursor()
results = cur.executemany("INSERT INTO Field (Field1) VALUES (?)", name)
conn.commit()

Requesting your assistance on this issue. Your help is much appreciated. Thank You.

2 Answers 2

2

New Answer

To suit your dataset more directly. You need a list of dictionaries, not a dictionary of lists because each dictionary has the column name as the key and a value associated with that column (key).

names = [{'name': "Freddy"}, {'name': "Joan"}]
results = c.executemany("INSERT INTO names ('name') VALUES (:name)", names)

The rest of the SQLite3 code working, this outputs:

Freddy
Joan

Option to Use a List of Tuples

You can also use a list of tuples. If you're not using a dictionary, you need to make sure each row is in tuple form. You need each of these tuples in a list. You need to call this list in the executemany function after the SQL query.

Essential code:

names = [("Freddy",), ("Joan",)]
results = c.executemany("INSERT INTO names (name) VALUES (?)", names)

Output:

Freddy
Joan

So make sure each entry is in tuple or dictionary form. Then put these tuples or dictionaries into a list. If it's in dictionary form, you cannot use the '?' placeholder. Use ':key_name' for each placeholder. (There might be a different option here, but I haven't found it yet).

Original Response

I suspect that executemany(...) should be execute(...). I haven't used SQLite3 in a while, so I'll test it now to make sure and then get back to you.

Update 1:

I remember the "?'s" needing to be sent in tuple form. I.e.

cur.executemany("INSERT INTO Field (Field1) VALUES (?)", name)

needs to be at least

cur.executemany("INSERT INTO Field (Field1) VALUES (?)", (name, ...))

But I've only gotten the execute() (and not executemany()) to work, with this, else it's back to the same error. So, here's my working code so far:

cur.execute("INSERT INTO Field (Field1) VALUES (?)", (name,))

It gave F, R, E, D, D, Y because it was reading each letter as values in the tuple. If it was in tuple form, it would read "Freddy" all in one go. But I'm still having issues with the executemany(), which I think works differently than we think it does.

Update 2:

Here's what I got to work. You need to make sure each row is in tuple form. You need each of these tuples in a list. You need to call this list in the executemany function after the SQL query.

Essential code:

names = [("Freddy",), ("Joan",)]
results = c.executemany("INSERT INTO names (name) VALUES (?)", names)

Full code:

import sqlite3

conn = sqlite3.connect('test.db')
c = conn.cursor()

# Create table
c.execute('''CREATE TABLE IF NOT EXISTS names
             (name text)''')

names = [("Freddy",), ("Joan",)]
results = c.executemany("INSERT INTO names (name) VALUES (?)", names)
conn.commit()

for row in c.execute('SELECT * FROM names'):
    print(row[0])

This prints out:

Freddy
Joan

Update 3:

I've updated it to suit your dataset more directly. You need a list of dictionaries, not a dictionary of lists because each dictionary has the column name as the key and a value associated with that column (key).

Here's some more code, I've gotten to work.

names = [{'name': "Freddy"}, {'name': "Joan"}]
results = c.executemany("INSERT INTO names ('name') VALUES (:name)", names)

With the rest of the code as it was, it outputs:

Freddy
Joan

(Inspired from this answer.)

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

1 Comment

Thanks Aaron, Your idea worked. I had the whole data into lists and there-after had them converted as tuples. Apart from that, I also had to change the sqlite function from executemany to execute and have that in a for loop.
1

you might consider to change

results = cur.executemany("INSERT INTO Field (Field1) VALUES (?)", name)

to

results = cur.executemany("INSERT INTO Field (Field1) VALUES (?)", (name))

since the expected parameter is a tuple, the string is not considered as a whole but splited

1 Comment

Hi, Thank You for your response. I did so, but there's no difference in output. I get the same thing.

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.