Last year, I wrote an article titled SQLite learning notes 1 (opening, operating, and shutting down databases, and implementing C Programs) using C language. I recently learned python, therefore, we have implemented the ptyhon implementation. The implementation is not described much, and the comments in the Code are very detailed. Paste it directly.
1. Implementation:
#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-# import logs and sqlite3 module import loggingimport logging. configimport sqlite3 # log configuration file name log_filename = 'logging. conf '# log statement prompt message log_content_name = 'sqlite _ log' # SQLite database name db_sqlite_path = ". \ dB \ sqlite_pytest.db "def log_init (log_config_filename, LOGNAME): ''' function: log module initialization function input: log_config_filename: log configuration file name lognmae: the prompt statement output: logger Author: Socrates Date: ''' logging before each log. config. fileconfig (log_config_filename) Logger = logging. getlogger (LOGNAME) return loggerdef operate_sqlite3_tbl_product (): ''' function: Operation sqlite3 database function input: None output: None Author: Socrates Date: 2012-02-11 ''' sqlite_logger.debug ("operate_sqlite3_tbl_product enter... ") # connect to the database try: sqlite_conn = sqlite3.connect (db_sqlite_path) handle T sqlite3.error, E: Print 'conntect SQLite database failed. 'sqlite_logger.error ("conntect SQLite database failed, ret = % s" % E. ARGs [0]) return sqlite_logger.info ("conntect SQLite database (% s) succ. "% db_sqlite_path) # obtain the cursor sqlite_cursor = cursor () # Delete the table SQL _desc2 =" Drop table if exists tbl_product3; "try: sqlite_cursor.execute (SQL _desc2) handle T sqlite3.error: print 'drop table failed' sqlite_logger.error ("Drop table failed, ret = % s" % E. ARGs [0]) sqlite_cursor.close () sqlite_conn.close () return sqlite_conn.commit () sqlite_logger.info ("Drop table (tbl_product3) succ. ") # create a table SQL _desc = ''' create table tbl_product3 (I _index integer primary key, sv_productname varchar (32); ''' try: sqlite_cursor.execute (SQL _desc) limit t sqlite3.error, E: print 'drop table failed. 'sqlite_logger.error ("Drop table failed, ret = % s" % E. ARGs [0]) sqlite_cursor.close () sqlite_conn.close () return sqlite_conn.commit () sqlite_logger.info ("create table (tbl_product3) succ. ") # insert record SQL _desc =" insert into tbl_product3 (sv_productname) values ('apple') "try: sqlite_cursor.execute (SQL _desc) failed t sqlite3.error, E: Print 'insert record failed. 'sqlite_logger.error ("insert record failed, ret = % s" % E. ARGs [0]) sqlite_cursor.close () sqlite_conn.close () return sqlite_conn.commit () sqlite_logger.info ("insert record into table (tbl_product3) succ. ") # query record SQL _desc =" select * From tbl_product3; "sqlite_cursor.execute (SQL _desc) for row in sqlite_cursor: Print row sqlite_logger.info (" % s ", row) # Close the cursor and database handle sqlite_cursor.close () sqlite_conn.close () sqlite_logger.debug ("operate_sqlite3_tbl_product leaving... ") if _ name _ = '_ main _': # initialize the log system sqlite_logger = log_init (log_filename, log_content_name) # operate the database operate_sqlite3_tbl_product ()
2. log information after running:
[2012-02-12 12:13:52,131 sqlite_log]DEBUG: operate_sqlite3_tbl_product enter... (test_log.py:39)[2012-02-12 12:13:52,147 sqlite_log]INFO: conntect sqlite database(.\db\sqlite_pytest.db) succ. (test_log.py:49)[2012-02-12 12:13:52,147 sqlite_log]INFO: drop table(tbl_product3) succ. (test_log.py:66)[2012-02-12 12:13:52,240 sqlite_log]INFO: create table(tbl_product3) succ. (test_log.py:83)[2012-02-12 12:13:52,365 sqlite_log]INFO: insert record into table(tbl_product3) succ. (test_log.py:97)[2012-02-12 12:13:52,365 sqlite_log]INFO: (1, u'apple') (test_log.py:104)[2012-02-12 12:13:52,365 sqlite_log]DEBUG: operate_sqlite3_tbl_product leaving... (test_log.py:110)
3. view through the command line:
Microsoft Windows XP [version 5.1.2600] (c) Copyright: 1985-2001 Microsoft Corp. c: \ Documents ents and Settings \ Socrates. WINXP-DUANYX> Cd/d e: \ study \ learning \ working program \ py_test \ SRC \ DBE: \ study \ learning \ working program \ py_test \ SRC \ dB> sqlite3.exe sqlite_pytest.dbsqlite version 3.7.9 2011-11-01 00: 52: 41 enter ". help "for instructionsenter SQL statements terminated with a"; "SQLite>. tablestbl_product3sqlite> select * From tbl_product3; 1 | applesqlite>. quite: \ study \ learning \ working program \ py_test \ SRC \ dB>