Writing SQLite3 database
Code: "io_sqlite_write.py". Programming language: Python DMelt Version 1. Last modified: 12/11/2015. License: Pro
https://datamelt.org/code/cache/io_sqlite_write_7733.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


# This example shows how to read SQLite database
# created using the native C++ code. Look at SQLjet for more examples. 
# S.Chekanov 
from org.tmatesoft.sqljet.core.table import SqlJetDb
from org.tmatesoft.sqljet.core import SqlJetTransactionMode
from java.io import File

dbFile=File("data.sqlite")
dbFile.delete()
db=SqlJetDb.open(dbFile, 1) # write database in "read mode"
db.getOptions().setAutovacuum(1)

db.beginTransaction(SqlJetTransactionMode.WRITE)
db.getOptions().setUserVersion(1)
query = "CREATE TABLE  ajax (name, value);"
db.createTable(query) 
table = db.getTable("ajax")
table.insert("Kitaev", "Alexander")
table.insert("Test", "Alexander")
table.insert("Test2", "Alexander")
db.commit()
db.close()

# read this databse
db = SqlJetDb.open(dbFile, 0);
print db.getSchema()
db.beginTransaction(SqlJetTransactionMode.READ_ONLY)
table=db.getTable("ajax")  # read one table
# open table
cursor=table.open()
# print number of fields
print cursor.getFieldsCount()
# print number of rows
print cursor.getRowCount()
db.close()

Ads help maintain this website.