python 数据库的增删改查+模块

来源:互联网 发布:java字符串占位符替换 编辑:程序博客网 时间:2024/05/20 05:29



import pymysql#查找数据库def findFromDB( query_id):    db = pymysql.connect(host='localhost', user='root', passwd='', db='test', port=3306, charset='utf8')    cursor = db.cursor()    sql = ' select * from amazon_aces where asin = %s '    cursor.execute(sql, (query_id))    db.commit()    cursor.close()    db.close()    return cursor.fetchone() is not None#插入数据库def insertIntoDB( asin, checked):    db = pymysql.connect(host='localhost', user='root', passwd='', db='test', port=3306, charset='utf8')    cursor = db.cursor()    sql = " insert into amazon_aces(asin , checked)  values(%s , %s) "    cursor.execute(sql, (asin, checked))    db.commit()    cursor.close()    db.close()def createTable():    '''创建表'''    db = pymysql.connect(host='localhost', user='root', passwd='', db='test', port=3306, charset='utf8')    cursor = db.cursor()    cursor.execute('''        CREATE TABLE `amazon_aces` (          `asin` varchar(200) NOT NULL ,          `checked` varchar(200) ,          PRIMARY KEY  (`asin`)        )         ''')    db.commit()    cursor.close()    db.close()#删除数据库表def dropTable():    db = pymysql.connect(host='localhost', user='root', passwd='', db='test', port=3306, charset='utf8')    cursor = db.cursor()    cursor.execute('''    DROP TABLE IF EXISTS `amazon_aces`    ''')    db.commit()    cursor.close()    db.close()if __name__ == '__main__':    dropTable()    createTable()    insertIntoDB("1","aaa")    insertIntoDB("2","bb")


模块开发:

import pymysqlimport pandas as pdimport reimport osimport requestsimport timeclass Database():    def __init__(self):        self.tablename = "category" # 可以设置表名        self.host = "localhost"        self.user = "root"        self.password ="123456"        self.database="ebay"        self.charset = "utf8"        self.connect = pymysql.connect(host = self.host, user = self.user,password = self.password, database = self.database, charset =  self.charset)        self.cursor = self.connect.cursor()    #删表    def dropTable(self):        sql = 'drop table if exists '+self.tablename        self.cursor.execute(sql)        print("删表")    #建表    def createTable(self):        sql = 'create table if not exists '+ self.tablename+ '''        (            id int(11) primary key auto_increment,            ebayno varchar(100) not null,            category varchar(1000)          )        '''        self.cursor.execute(sql)        print("建表")    #判断是否存在ebayno    def is_exists_ebayno(self,ebayno):        sql = 'select * from '+self.tablename + ' where ebayno = %s'        self.cursor.execute(sql,ebayno)        if self.cursor.fetchone() is None:            return False        return True    #保存数据    def save(self,ebayno,category):        sql = 'insert into '+self.tablename+' (ebayno , category) values (%s,%s)'        self.cursor.execute(sql,(ebayno,category))        self.connect.commit()db = Database()db.dropTable()db.createTable()print(db.is_exists_ebayno("111"))db.save("111","adaa")print(db.is_exists_ebayno("111"))



原创粉丝点击