0

I am basically having issues with the syntax and can't find much information anywhere. I have two tables. Category_tb and Information_tb.

Category_tb Has column of category_name(text). Information_tb Has title(text) and category_id(INTEGER)

I am not using the foreign key I am using ROWID of Category_tb. Here are the SQL quires. Basically I am having a problem with the insertion of data into Information_tb using Category_tb.ROWID as a foreign key.

title = 'Some title'
categories = 'Crime'
self.curr.execute("""
                    insert into information_tb values (?,?)""",(
                        [title],
                        ["""select ROWID from Category_tb where Category_tb.category_name = [categories] """]
                        ))

Any help would be appreciated. Thanks in advance.

1 Answer 1

1

The values clause of the insert statement should be column names. It can't contain a select statement. However, you can specify the values with a select statement.

Try this:

self.curr.execute("insert into information_tb (title, category_id) select ?, ROWID from Category_tb where Category_tb.category_name = ?",
      (title, categories))

Remember to commit afterward to save the changes.

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

4 Comments

Not exactly what I wanted but I got a general idea. I wanted to get the ROWID of the category the that matched categories and inserted that ID into category_id of information_tb. I worte this quries but still getting an error, please take a look at it. self.curr.execute(""" insert into information_tb (title, category_id) Values (?,select ROWID from category where category.category = ?)""" ,(title, categories[0])) Getting an error "sqlite3.OperationalError: near "select": syntax error"
@ahsanmukhtar You don't need values (.... Please copy the code I've given.
I did I got this SQLite error sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.
@ahsanmukhtar You have to enclose the values in parentheses: (title, categories). The brackets are important.

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.