Creating a table

Now, we are going to create a table in our database. For that, we will create a create_table.py script and write the following content in it:

import sqlite3

con_obj = sqlite3.connect("test.db")
with con_obj:
cur_obj = con_obj.cursor()
cur_obj.execute("""CREATE TABLE books(title text, author text)""")

print ("Table created")

Run the script and you will get the output as follows:

student@ubuntu:~/work $ python3 create_table.py

Output:
Table created

In the preceding example, we created a table books using a CREATE TABLE statement. First, we established a connection with our database using test.db. Next, we created a cursor object that we used to execute the SQL query on our database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset