0

My python 3.6 code is supposed to create a database and create a table inside it.

import sqlite3

db_filename = 'database.db'
connect = sqlite3.connect(db_filename)

c = connect.cursor()

c.execute('CREATE TABLE IF NOT EXISTS task (id number PRIMARY KEY, priority integer, details text, status text)')

connect.commit()
connect.close()

However the output is not what I intended. I am getting weird characters included in the .db file;

SQLite format 3   @                                                                     .�
� b b�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  k�9tabletasktaskCREATE TABLE task (id number PRIMARY KEY, priority integer, details text, status text)'; indexsqlite_autoindex_task_1task       
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                 

If anyone could tell me where I went wrong I would be grateful.

Thanks.

jez
  • 3
  • 1

1 Answers1

2

There is nothing wrong here. To view a .db file you need db viewer or reader tool. http://sqlitebrowser.org/ has DB browser for SQLite which can be used to view your database. You can install it and use it to read your .db file. If you want to use the table you can do so by inserting elements in the table and viewing it as follows:

import sqlite3
db_filename = 'database.db'
connect = sqlite3.connect(db_filename)

c = connect.cursor()

c.execute('CREATE TABLE IF NOT EXISTS task (id number PRIMARY KEY, priority integer, details text, status text)')
c.execute("INSERT INTO task (id,priority,details,status) \
      VALUES (1,22,'ABC','YES' )");
cursor = c.execute("SELECT id,priority,details,status from task")
for row in cursor:
   print ("ID = ", row[0])
   print ("PRIORITY = ", row[1])
   print ("DETAILS = ", row[2])
   print ("STATUS = ", row[3], "\n")
connect.commit()
connect.close()

OUTPUT:
ID =  1
PRIORITY =  22
DETAILS =  ABC
STATUS =  YES
Vallie
  • 689
  • 9
  • 19
  • What if it wont open it correctly? When I open the file with sqliteBrowser, it creates the table but doesn't add the data – paty.r15 Aug 21 '20 at 17:31
  • Can you add a new question and attach a screenshot of the sqliteBrowser view you see? You can paste the link to your question here – Vallie Aug 30 '20 at 14:17
  • Hello @Vallie ,here is the link to my question related to sqlite issue: https://stackoverflow.com/questions/63816015/cant-retrieve-data-from-sqlite3-file-with-unexpected-characters – paty.r15 Sep 09 '20 at 16:48