Do you need to add data to your SQLite database inside your Python project?
Do you have a DataFrame in your Python project, or do you want to add the data manually? Check out this post here called Pandas DataFrame to SQLite.
Would you like to use an INSERT INTO statement in Python to put your data into a SQLite database? Let’s create a project to do this. My Python project is called Python Insert Into SQLite. We are going to ask the user at run time. I will respond with Jane Foster and Researcher.
4 | position = input ( "Position? " ) |
6 | connection = sqlite3.connect( 'D:\Test\emp.db' ) |
7 | cursor = connection.cursor() |
10 | cursor.execute( , ( 5 , name, position)) |
Name? Jane Foster
Position? Researcher
Using the syntax with the question marks help prevent SQL injection attacks.
What if we wanted to enter in several at the same time? We still are doing this manually in Python code.
3 | connection = sqlite3.connect( 'D:\Test\emp.db' ) |
4 | cursor = connection.cursor() |
9 | employee_list = [( 6 , 'Herbert' , 'Boot Salesman' ), |
10 | ( 7 , 'Rosemary' , 'Architect' )] |
11 | cursor.executemany( , employee_list) |
We use executemany instead of execute.