Pyrhon中Mysql数据库的操作
来源:互联网 发布:金税盘如何备份的数据 编辑:程序博客网 时间:2024/05/21 09:17
mysql基础设置
1.下载
yum install mariadb-server MySQL-python -y
sytemctl start mariadb
mysql_secure_installation
2.utf-8
vim /etc/my.cnf
charset = utf-8
systemctl restart mariadb
python操作mysql
MySQL连接操作
##导入模块import MySQLdb##建立数据库连接conn = MySQLdb.connect(host='localhost',user='root',passwd='westos')hostname 主机名 user 用户 passwd 密码 charset='utf8' 数据库编码##创建一个数据库游标cur = conn.cursor()##提交操作(在上传数据后使用)conn.commit()##关闭游标cur.close()##断开连接conn.close()
游标操作
# cur.execute游标操作
##创建表
In [4]: cur.execute('use python716')Out[4]: 0LIn [5]: cur.execute('create table userinfo(username varchar(20),passwd varchar(20));')Out[5]: 0L
##快速插入多条数据
sqli = “insert into userinfo values(%s,%s)”
cur.executemany(sqli,[(),(),…]) ##插入多条数据(以列表插入)
cur.execute(sqli,()) ##插入单条数据
In [8]: cur.execute('use python716') ##进入数据库Out[8]: 0LIn [9]: sqli = "insert into userinfo values(%s,%s)"In [10]: cur.executemany(sqli,[('a',1),('b',2)]) ##插入数据Out[10]: 2LIn [11]: cur.executemany(sqli,[('c',1),('d',2)])Out[11]: 2LIn [12]: conn.commit() ##提交插入的数据
##查询数据
cur.execute(‘select * from userinfo’)
cur.fetchall(self): 接收全部的返回结果行
cur.fetchmany(size=None): 接收 size 条返回结果行 . 如果 size 的值大于返回的结果行的数量 , 则会返回 cursor.arraysize 条数据 .
cur.fetchone(): 返回一条结果行
##移动游标
scroll(value, mode=’relative’): 移动指针到某一行 .
如果 mode=’relative’, 则表示从当前所在行移动 value 条 ;
如果 mode=’absolute’, 则表示从结果集的第一行移动 value 条;
In [13]: cur.execute('select * from userinfo') ##查看表Out[13]: 4LIn [14]: cur.fetchall() ##查看所有结果Out[14]: (('a', '1'), ('b', '2'), ('c', '1'), ('d', '2'))In [15]: cur.fetchall() ##由于游标已经到结尾,所以返回值为空Out[15]: ()In [16]: cur.scroll(0,'absolute') ##移动游标到起点
阅读全文
0 0
- Pyrhon中Mysql数据库的操作
- pyrhon连接oracle数据库
- MySql数据库中Table的基本操作
- PHP中MYSQL数据库的操作
- MATLAB中关于MySQL数据库的操作
- Python中对mysql数据库的操作
- QT中对 mysql数据库的操作
- linux中mysql数据库的基本操作
- Pyrhon赋值语句后逗号的作用
- Pyrhon集合与词典常用的方法
- mysql的数据库操作
- MySQL数据库的操作
- MySQL数据库的操作
- mysql数据库的操作
- MySql数据库的操作
- mysql 数据库的操作
- shell中操作mysql数据库
- Qt中MySQL数据库操作
- 快捷键让SublimeText在编文件快速在浏览器打开
- Maven中的jeety插件运行报错:No plugin found for prefix 'jeety' in the current project and in the plugin group
- Java中没有抽象方法的抽象类
- Tourist Attractions
- Android使用LruCache、DiskLruCache实现图片缓存+图片瀑布流
- Pyrhon中Mysql数据库的操作
- ConcurrentHashMap
- Android 动图 WebP Gif 的 播放 和 暂停
- Unity中设计模式应用(二):结合中介者模式的观察者模式
- textView属性
- 关于SPI协议中MCU与FPGA片间通信的理解(FPGA作为从机)
- 分布式版本控制系统-Git
- Python3 调用Sentec相机SDK方法
- maximum average value