In SQLite database we use the following syntax to create a table: CREATE TABLE databasename.tablename ( column1 datatype PRIMARY KEY (one or more columns), column2 datatype, column3 datatype. I missed something originally that I found from testing my code. In this article, we will discuss how can we create tables in the SQLite database from the Python program using the sqlite3 module. It returns a Connection object that represents the database. The connect () function opens a connection to an SQLite database. Inside the function, we call the connect () function of the sqlite3 module. You would likely benefit more from having a single class with methods because your second class is basically useless and is very closely related to your first class anyways: class DB:ĭef hello(self): # making a method instead In this code: First, we define a function called createconnection () that connects to an SQLite database specified by the database file dbfile. H = Hello(db) # now we need to feed this instanceĪlthough this probably isn't the best way to go about it. # ^^ we remove this line because now this is the defaultĭb = DB('test.db') # this makes an instance of your DB class and calls it "db" # db = DB() # Create db or connect to existing one Any class would use it then: class Hello: def hi (self): db DB () Create db or connect to existing one cursor db.connection.cursor () Now, when testing, I pass on a test database: db DB ('test.db'). nnection = nnect(dbname)ĭef hi(self, db=DB()): # we make it have a default db of DB() (which in turn defaults to 'mydb.db') class DB: def init (self, dbname'mydb.db'): try: nnection nnect (dbname) except: print ('Error') finally: pass. If you want to pass the instance of your DB class ( db) you would need to feed the instance to your Hello class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |