0

I have this simple code which I can't make work.

import sqlite3

conn = sqlite3.connect('db\\books')
c = conn.cursor()

col = []

title = input('title')
text = input('text')
tags = input('tags')

col.append(title)
col.append(text)
col.append(tags)

c.executescript("INSERT INTO books (title,text,tags) "
                "VALUES (col[0],col[1],col[2])")

The db code (connection and normal insert) works but the problem rise when I want to do what you see above.

The goal I would like to achieve is to let the user insert the data into db (all strings). I don't know if this is the right way to do this...

How can I do this ?

Thanks

2 Answers 2

3

One option is to change your last line to:

c.execute("INSERT INTO books (title,text,tags) VALUES (?,?,?)", (col[0], col[1], col[2]))

And then commit and close the connection if you're done making changes:

conn.commit()
conn.close()
Sign up to request clarification or add additional context in comments.

4 Comments

TypeError: function takes exactly 1 argument (2 given)
c.execute() definitely takes two arguments, as per the docs. Adrian Tam's example looks good to me too. Are you sure you're calling the correct function?
In fact, the error comes out using c.executescript(). Using c.execute shows no error but it don't do anything...
Understood the error...I didn't copied conn.commit(). Thanks to all.
3

This line:

c.executescript("INSERT INTO books (title,text,tags) "
                "VALUES (col[0],col[1],col[2])")

Is not a valid SQL. Now since c is defined as a cursor, you can run execute from it directly instead of executescript, which the latter is suppose to create a cursor from a connection and execute a SQL. Just replace that line with the following should work:

c.execute("INSERT INTO books (title,text,tags) "
          "VALUES (?,?,?)", col)

The SQL above uses a "qmark style" placeholder, which takes actual value from the parameter list that follows it.

1 Comment

TypeError: function takes exactly 1 argument (2 given)

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.