Python使用sqlite3库模拟Mysql存储文件
import sqlite3
class SqliteDB(object):
"""
在本地创建一个虚拟的Mysql文件
"""
def __init__(self, db_path=r'C:\Users\evaliu\Desktop', db_name='sql_lite3'):
self.db_path = db_path + '\\' + db_name
self.cxn = sqlite3.connect(self.db_path)
self.cur = self.cxn.cursor()
def create_table(self):
try:
self.cur.execute('''
CREATE TABLE users(
uid INTEGER,
prid INTEGER)
''')
except Exception as e:
print('Create Table Error, ERROR-MSG: [ {} ]'.format(e))
print('Recreate the users now!')
self.cur.execute('DROP TABLE users')
self.create_table()
def read_table(self):
print('Mysql dump:')
self.cur.execute('SELECT * FROM users')
for each_line in self.cur.fetchall():
print(each_line)
if __name__ == '__main__':
mysql = SqliteDB()
try:
mysql.create_table()
mysql.cur.execute('INSERT INTO users VALUES(100, 200)')
mysql.read_table()
finally:
mysql.cur.close()
mysql.cxn.commit()
mysql.cxn.close()