py2psql.py
Dependency
- psycopg2 (PostgreSQL + Python)
Constructor
- constructed parameter:
- host : URL or IP
- port : postgresql server port
- db : database name as a string
- tb : table name as a string
- user : login user as a string
- pwd : the password as a string
def __init__(self, getHost, getPort, getDB, getTB, getUser, getPwd):
API
- SELECT operation
- param@getConds : {}, defined where SQL conditions
- param@getParams : [], selected column names, empty : means all
- param@asdict : boolean, returned row as dictionary
- data as [] type
- also support status object, use status()
def select(self, getConds, getParams, asdict=False):
- UPDATE operation
- param@getParams : {}, set sql parameters
- param@getConds : {}, where sql conditions
- retn : 0 : failure, 1 : success
- note : also support status object, use status()
def update(self, getParams, getConds)
- INSERT operation
- param@getParams : {}, value sql parameters
- retn : 0 : failure, 1 : success
- note : also support status object, use status()
def insert(self, getParams)
- DELETE operation
- param@getConds : {}, where sql conditions
- retn : 0 : failure, 1 : success
- note : also support status object, use status()
def delete(self, getConds)
- Custom SQL operation
- param@getSQL : parameter-based complex sql command,
e.g. "select * from public.user where name = %(name)s;"
- param@hasRetValue : are there returned values ?
- param@getParams : {}
e.g. {'name' : "test114"}
- param@asdict : only works when param@hasRetValue is true, returned value as dictionary data type
- retn : return executing status
def execsql(self, getSQL, hasRetValue, getParams, asdict=True)
- Get Table schema
- param@getTable : get desired table schema
- retn : status object
- note : when param@getTable == None, self.__tb must exist at the beginning of object creation
def getTableSchema(self, getTable=None)
- Get the latest operation execution status
def status()
- Create table : create table based on schema
- param@tableName : name of the table for creation
- param@tableSchema : { 'colName' : 'colSchema', '' : '' }
- param@dropFirst : whether to drop table first if it exists
- retn : None, call status() to get status object
def createTable(self, tableName, tableSchema, dropFirst=False)
- Alter table : alter table schema
- param@tableName : table for altering
- param@tableSchema : { 'colName' : 'new col schema' }
- param@createTableFirstIfNotExisted : whether to create table first if table does not exist
- param@addColIfNotExisted : whether to add column if it does not exist
- param@theSameWithThisSchema : whether to fit the table with the input schema
- retn : None, call status() to get status object
- note : if addColIfNotExisted == False, the column for altering would be skipped
def alterTable(self, \
tableName, \
tableSchema, \
createTableFirstIfNotExisted=True, \
addColIfNotExisted=True,\
theSameWithThisSchema=True)
- Drop table
- param@tableName : table for droping
- retn : None, call status() to get status object
def dropTable(self, tableName)
Example
p2l = py2psql("127.0.0.1","5432","ckan_default","public.user","ckan_default","ckan")
p2l = py2psql("127.0.0.1","5432","ckan_default","public.user","ckan_default","ckan")
p2l.getTableSchema()
p2l2 = py2psql("127.0.0.1","5432","ckan_default","","ckan_default","ckan")
p2l2.getTableSchema("public.user",0)
p2l2.getTableSchema("public.user",-1)
p2l.status()
data = p2l.select({where},[columns])
data = p2l.select({"name":"test114"},["name","email","123"])
data = p2l.select({"name":"test114"},[])
data = p2l.select({where},[columns],asdict=True)
data = p2l.select({"name":"test114"},["name","email","123"],asdict=True)
data = p2l.select({"name":"test114"},[],asdict=True)
p2l.update({set},{where})
p2l.update({"email":"test@tw"},{"name":"test114"})
p2l.insert({ data })
p2l.insert({ "id" : "acbdhcbdh-abchdbch", "name":"123","email":"123@tw" })
p2l.delete({where})
p2l.delete({"name":"test1", "email":"test1@tw"})
p2l.execsql("sql command", is there returned value, {parameter : value})
p2l.execsql("select * from public.user where name = %(name)s;", True, {'name' : "test114"}, True)
p2l.status()
p2l.execsql("select * from information_schema.tables where table_name = %(name)s;", True, {'name' : "user"})
p2l.status()
p2l.createTable("test", {"id" : "serial primary key", "context" : "text not null"}, dropFirst=True)
p2l.status()
p2l.alterTable("test", {"context" : "text"}, createTableFirstIfNotExisted=False, addColIfNotExisted=False, theSameWithThisSchema=True)
p2l.status()
p2l.dropTable("test")
p2l.status()