db_zczc.html


001| '''\
001| Database Table classes.
001| 
001| classes:
001|     dbSomeTable (dbTable)
001| 
001| Developer@Sonnack.com
001| June 2017
001| '''

002| ####################################################################################################
003| from dbsql import dbTable
004| ####################################################################################################
005| BasePath = r'C:\CJS\prj\Python\lib\test'
006| DataPath = BasePath
007| 
008| 
009| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
010| class dbBaseTable (dbTable):
011|     '''Base class for tables.'''
012|     dbName = 'example.sqlite'
013|     dataPath = DataPath
014| 
015|     def __init__(self, tab_name, tab_cols):
016|         dbTable.__init__(self, path.join(self.dataPath,self.dbName), tab_name, tab_cols)
017| 
018| 
019| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
020| class dbExampleTable (dbBaseTable):
021|     tabName = 'example'
022|     tabCols = ['"rcd_id" INTEGER NOT NULL UNIQUE'
023|               ,'"grp_id" INTEGER NOT NULL'
024|               ,'"sec_id" INTEGER NOT NULL'
025|               ,'"title" TEXT NOT NULL'
026|               ,'"subject" TEXT NOT NULL'
027|               ,'"message" TEXT DEFAULT NULL'
028|               ,'"ts" REAL NOT NULL'
029|               ,'"px" REAL DEFAULT NULL'
030|               ,'"py" REAL DEFAULT NULL'
031|               ]
032|     tabData = []
033| 
034|     def __init__(self):
035|         dbBaseTable.__init__(self, self.tabName, self.tabCols)
036| 
037|     def create_table (self):
038|         self.Log.info('Create Example Table')
039|         self.db_create()
040|         self.load_records()
041| 
042|     def destroy_table (self):
043|         dbBaseTable.destroy_table(self, 'Destroy Example Table')
044| 
045|     def load_records (self):
046|         self.tabData = []
047| 
048|     def list_to_file (self, fname='example.out'):
049|         self.Log.debug('db[%s] ListToFile' % self.name)
050|         sql = 'SELECT * FROM "%s" ORDER BY ts' % self.name
051|         fn = path.join(BasePath, fname)
052|         fp = open(fn, mode='w', encoding='utf-8')
053|         try:
054|             rs = self.db_query(sql)
055|             if not rs: Log.warn('No data!'); return []
056|             for r in rs:
057|                 print(r, file=fp)
058|             print(file=fp)
059|             self.Log.info('wrote: %s' % fn)
060|         except:
061|             raise
062|         finally:
063|             fp.close()
064| 
065| 
066| 
067| ####################################################################################################
068| '''eof'''
069|