# Drop any existing tables with the same nameĬursorObject.execute("drop table billing.cashbill") # Create database connection to the sqlite main databaseĬonnectionObject = nnect("primedb.db")ĭone with prime db and want to work on billing database using the same database connectionĪttachDatabaseSQL = "ATTACH DATABASE ? AS billing"ĬursorObject.execute(attachDatabaseSQL,dbSpec) # Example Python Program to attach a database file to an existing DB connection The SQLite table is queried and the results are displayed before detaching the database and closing the connection.Įxample3- Attaching a SQLite table using Python:.The example Python program below attaches a new SQLite database, creates a new table on the attached database and populates two records into the new table.Once the ATTACH DATABASE is executed, the connection object obtained can now be used to access both the main database and the newly attached database. Through the connection object a cursor object should be obtained using which any SQL statement including the ATTACH DATABASE statement.Once Sqlite3 is imported into a python program a connection object can be created to the SQLite main database.The Sqlite3 module acts as the SQLite client using which a Python program can connect to the Sqlite3 database.INSERT INTO billing.cashbill VALUES (2, 272,100,3,300,2,6,306)Īttaching a SQLite Database using Python: The following INSRT statements will populate the newly attached SQLite database billing.cashbill with two records. The above example will create a database named billing inside the file bill.db and any tables created on the database billing will be saved under the file bill.db.Įxample 2- Create a new table in the attached SQLite Database:Īssuming the currency is stored in the lowest denomination such as cents and tax rate is stored as basis points the following statement will create a table with the name cashbill in the SQLite database billing.ĬREATE TABLE billing.cashbill(id int, itemcode int, rate int, quantity int, price int, taxrate int, tax int, total int) If a database file with the specified file name does not exist a new file will be created.The ATTACH statement in SQLite is of the form, A database file can be attached to an existing SQLite connection by using the ATTACH statement.The “main” database and “temp” database can neither be attached nor be detached.The “temp” database is for storing temporary tables.One is the “main” database and another one is the “temp” database. SQLite has two databases, which are always loaded onto it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |