python+sqlite3 实现简单操作sqlite数据库的demo

sqlite功能简约,小型,是一个自包含、基于文件的数据库。它的功能直接被集成在数据库文件中,应用会直接访问包含数据的文件(即SQLite数据库),而不是通过一些端口(port, socket)来交互,只需要在本地创建一个虚拟的sqlite文件即可。

当然,要满足多用户同时访问,或者是网站访问量比较大是使用MYSQL比较合适。

下面用python+sqlite3实现一个简单操作sqlite数据库的demo
import sqlite3
import os

server_root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


class ConnSql(object):
	"""
    不需要连接真实的Mysql数据库,在本地创建一个虚拟的Mysql文件
    """
    def __init__(self, db_path=r'{}'.format(server_root_path), db_name='sql_test_lite3.db'):
        self.db_path = '{}\\{}'.format(db_path, db_name)
        self.cxn = None
        self.cur = None

    def conn_db(self):
        self.cxn = sqlite3.connect(self.db_path)
        self.cur = self.cxn.cursor()
        print('connect ok!')

    def close_db(self):
        if self.cxn and self.cur:
            self.cur.close()
            self.cxn.close()
            print('close ok!')

    def create_table(self, create_teble_sql):
        # self.cur.execute('''
        #     CREATE TABLE users(
        #         id INTEGER primary key,
        #         name varchar(20)
        #         )
        #     ''')
        self.cur.execute(create_teble_sql)

    def search_info(self, search_sql):
        # get_info = self.cur.execute('''
        #         select * from users
        #     ''')
        get_info = self.cur.execute(search_sql)
        print(get_info)
        return get_info

    def update_info(self, update_sql):
        # self.cur.execute('''
        #         insert into users (name) values ('test_name1')
        #     ''')
        self.cur.execute(update_sql)
        self.cxn.commit()
        print('update info ok!')

    def show_table(self):
        """
        sqlite3 存在一个系统表 sqlite_master,结构如下:
        sqlite_master(
            type TEXT,
            name TEXT,
            tbl_name TEXT,
            rootpage INTEGER,
            sql TEXT
        )
        通过这个表,可以查看当前数据库有哪些表
        """

        get_table_sql = """select * from sqlite_master where type='table' """
        tables_info = self.cur.execute(get_table_sql)
        # print(self.cur.fetchall())

        result = []
        for table_info in tables_info:
            print('table name is [%s] ' % table_info[1])
            result.append(table_info[1])

        print('show table ok!')
        return result


def sql_main():
    sql_obj = ConnSql()
    sql_obj.conn_db()
    # sql_obj.create_table(create_teble_sql='''
    #         CREATE TABLE test_info(
    #             id INTEGER primary key,
    #             uuttype varchar(20),
    #             sn varchar(20),
    #             product_name varchar(20),
    #             date date(20)
    #             )
    #         ''')
    # sql_obj.update_info(update_sql='''
    #                 insert into test_info (uuttype,sn,product_name,date) values
    #                 ('74-123646-01','foc23264602','bermuda','19-06-02')
    #                 ''')
    # info = sql_obj.search_info(search_sql='''select * from test_info''')
    # for i in info:
    #     print(i)
    sql_obj.show_table()

    sql_obj.close_db()


if __name__ == '__main__':
    sql_main()

最后在数据库中生成的结果是:
python+sqlite3 实现简单操作sqlite数据库的demo_第1张图片

你可能感兴趣的:(sqlite3,python,python,sqlite,sqlite3)