pymysql连接mysql

pymysql的连接

连接数据库

import pymysql

settings = {
    'host':'192.168.137.108',
    'port':3306,
    'user':'tornado',
    'password':'tornado',
    'db':'tornado',
    'charset':'utf8'
}
conn = pymysql.connect(**settings)

print(conn)
#<pymysql.connections.Connection object at 0x000001D3141E5C88>

cursor = conn.cursor()
cursor.execute('show databases')
# one = cursor.fetchone()              #取出一条数据
all = cursor.fetchall()              #取出所有数据
# print(one)
print(all)

table = '''
create table user(
  id int PRIMARY KEY auto_increment,
  username VARCHAR(20) not null,
  password VARCHAR(20) not null
)
'''

'''
显示sql语句的方法一
mysql> show create table user;
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                  |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user  | CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


显示sql语句方法二
mysql> desc user;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| username | varchar(20) | NO   |     | NULL    |                |
| password | varchar(20) | NO   |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

'''
# cursor.execute(table)  #创建完成后注释掉,否则报错表存在

插入值

### 插入值
sql = 'insert into user(username,password) values (%s,%s)'
# cursor.execute(sql,('李四','qwe123'))
# cursor.executemany(sql,[('王五','abc123'),('赵六','333444')])
# conn.commit()
'''
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  2 | zhangsan | qwe123   |
|  3 | zhangsan | qwe123   |
|  4 | 李四     | qwe123   |
|  5 | 王五     | abc123   |
|  6 | 赵六     | 333444   |
+----+----------+----------+

'''

修改值

sql2 = 'update user SET username=%s where id=%s'
# cursor.execute(sql2,('张三',2))
# conn.commit()
'''
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  2 | 张三     | qwe123   |
|  3 | zhangsan | qwe123   |
|  4 | 李四     | qwe123   |
|  5 | 王五     | abc123   |
|  6 | 赵六     | 333444   |
+----+----------+----------+

'''

删除值

sql3 = 'delete from user  WHERE id=%s'
cursor.executemany(sql3,[(3,),(6,)])
conn.commit()

'''
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  2 | 张三     | qwe123   |
|  4 | 李四     | qwe123   |
|  5 | 王五     | abc123   |
+----+----------+----------+

'''
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值