can't insert data into sqlite3 using python
Question: I can successfully create table in sqlite3 database using Python but I can't
insert data to it

    
    
    # coding: utf-8
    
    import sqlite3
    
    text = "Welcome"
    
    def cur_execute(data):
        con = sqlite3.connect('sqlite3.db')
        try:
            with con:
                cur = con.cursor()
                cur.execute(data)
                con.commit()
        except Exception as why: 
            print(why)
        finally:
            if con: con.close()
    
    cur_execute("CREATE TABLE Hello(test TEXT)")
    cur_execute("INSERT INTO Hello VALUES(?)", (text))
    

show to me this error : cur_execute() takes exactly 1 argument (2 given)

thx.


Answer: @zoosuck answer works, but it also removed the sql sanitation built in for the
library. You can can use the built in sql sanitization by implementing the
following:

    
    
    import sqlite3
    
    text = "Welcome"
    
    def cur_execute(data, args=()):
        con = sqlite3.connect('sqlite3.db')
        with con:
            cur = con.cursor()
            cur.execute(data, args)
            con.commit()
    
    cur_execute("CREATE TABLE Hello(test TEXT)")
    cur_execute("INSERT INTO Hello VALUES(?)", (text, ))
    

Please note that with the `with` statement, you do not need to close the
connection. I removed the try catch around the connection to show the full
error if errors do occur.

Additionally, there was a problem with your original code. The second argument
in `cur_execute("INSERT INTO Hello VALUES(?)", (text))` needs to be a
tuple/list.

