使用Python3对SQLite数据库插入,查询操作:

使用Python3对SQLite数据库插入,查询操作:

import sqlite3,sys


class GetSQLite(object):

    def __init__(self,db_path):
        self.conn = sqlite3.connect(db_path)
        print("Opened database successfully")
        self.curs = self.conn.cursor()

    def insert_data(self,sql):
        self.curs.execute(sql)
        print("Data write in successfully")
        self.conn.commit()
        #self.conn.close()

    def select_data(self,sql):
        self.curs.execute(sql)
        print("Data select successfully")
        res = self.curs.fetchall()
        return res


if __name__ == '__main__':
    sql1 = "insert into imglink (type,url) values ('img','http://www.baidu.com')"
    sql = "select * from sqlite_sequence;"
    db_path = sys.path[0] + '/getlink.db'
	GetSQLite(db_path).insert_data(sql1)
    cc = GetSQLite(db_path).select_data(sql)

    print(cc)

 
 

你可能感兴趣的:(python学习)