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"))
阅读全文
0 0
- python 数据库的增删改查+模块
- python(数据库的增删改查)
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删查改
- 数据库的增删查改
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- 数据库的增删改查
- MyEclipse代码提示卡死优化
- Spring缺少aspectjweaver.jar异常
- 最常用的15大Eclipse开发快捷键技巧
- vim操作指令
- 微服务架构(Microservice Architecture)
- python 数据库的增删改查+模块
- Faster-RCNN+ZF用自己的数据集训练模型(Python版本)
- 二次同余式(草稿)
- React基础(未完结)
- 【Cython】Cython在Windows环境下的部署安装
- 最好用的剧情插件:USequencer初识
- 将BLOB类型值在富文本显示和将富文本内容以BLOB类型存储到数据库
- SourceTree 免登录跳过初始设置
- html复杂表格