Python Crud column names
Python 2.7 python27 Python Scaffolding
Create a directory and go there
virtualenv vedir source vedir/bin/activate
Let’s get some things
pip install sqlalchemy
Let’s use reflection for models, make a models.py
from sqlalchemy import create_engine, Table from sqlalchemy.orm import scoped_session,sessionmaker from sqlalchemy.ext.declarative import declarative_base import sqlite3 class DBSession1(): def __init__(self): self.engine1= create_engine('sqlite:///adatabase.sqlite3') self.Base1 = declarative_base() return None def init_sqlite3(dbname): conn = sqlite3.connect(dbname) c = conn.cursor() c.execute("DROP TABLE IF EXISTS customer") c.execute(""" CREATE TABLE customer (id INTEGER NOT NULL, customer CHAR(6), name VARCHAR(255), address1 VARCHAR(255), address2 VARCHAR(255), city VARCHAR(255), zip VARCHAR(255), phone1 VARCHAR(255), phone2 VARCHAR(255), PRIMARY KEY(id))""") conn.commit() c.execute(""" INSERT INTO customer ('customer','name','city','zip') VALUES ("111111","Alice Cooper", "Decatur", "30030")""") conn.commit() return conn if __name__=="__main__": init_sqlite3("adatabase.sqlite3") x = DBSession1() customers = Table('customer',\ x.Base1.metadata, autoload=True,\ autoload_with=x.engine1) print customers.columns
You can comment and uncomment the init_sqlite3 to create a database, or better yet, execute that function from an init_db.py with import models; models.init_sqlite3(“adatabase.sqlite3”)
Decide how you want to centralize the name of the sqlite3 file for your application.