当前位置:Gxlcms > 数据库问题 > python3.6 与MYSQL的安装与连接

python3.6 与MYSQL的安装与连接

时间:2021-07-01 10:21:17 帮助过:51人阅读

 
  1. mysql> create database mydb;  

 

 

刚开始测试这个例子时用创建了一个money的表,但是在python编译时出现

所以重新对其创建了一个save.money(主要要把之前的表给删了,不然会出现错误),我用了‘id‘,出现了语法错误,所以直接id就行,不要带引号。
[python] view plain copy  
  1. import pymysql  
  2.   
  3. # 连接数据库  
  4. connect = pymysql.Connect(  
  5.     host=‘localhost‘,  
  6.     port=3306,  
  7.     user=‘root‘,  
  8.     passwd=‘1234‘,  
  9.     db=‘save‘,  
  10.     charset=‘utf8‘  
  11. )  
  12.   
  13. # 获取游标  
  14. cursor = connect.cursor()  
  15.   
  16. # 插入数据  
  17. sql = "INSERT INTO money (name, account, saving) VALUES ( ‘%s‘, ‘%s‘, %.2f )"  
  18. data = (‘雷军‘, ‘13512345678‘, 10000)  
  19. cursor.execute(sql % data)  
  20. connect.commit()  
  21. print(‘成功插入‘, cursor.rowcount, ‘条数据‘)  
  22.   
  23. # 修改数据  
  24. sql = "UPDATE money SET saving = %.2f WHERE account = ‘%s‘ "  
  25. data = (8888, ‘13512345678‘)  
  26. cursor.execute(sql % data)  
  27. connect.commit()  
  28. print(‘成功修改‘, cursor.rowcount, ‘条数据‘)  
  29.   
  30. # 查询数据  
  31. sql = "SELECT name,saving FROM money WHERE account = ‘%s‘ "  
  32. data = (‘13512345678‘,)  
  33. cursor.execute(sql % data)  
  34. for row in cursor.fetchall():  
  35.     print("Name:%s\tSaving:%.2f" % row)  
  36. print(‘共查找出‘, cursor.rowcount, ‘条数据‘)  
  37.   
  38. # 删除数据  
  39. sql = "DELETE FROM money  WHERE account = ‘%s‘ LIMIT %d"  
  40. data = (‘13512345678‘, 1)  
  41. cursor.execute(sql % data)  
  42. connect.commit()  
  43. print(‘成功删除‘, cursor.rowcount, ‘条数据‘)  
  44.   
  45. # 事务处理  
  46. sql_1 = "UPDATE money SET saving = saving + 1000 WHERE account = ‘18012345678‘ "  
  47. sql_2 = "UPDATE money SET expend = expend + 1000 WHERE account = ‘18012345678‘ "  
  48. sql_3 = "UPDATE money SET income = income + 2000 WHERE account = ‘18012345678‘ "  
  49.   
  50. try:  
  51.     cursor.execute(sql_1)  # 储蓄增加1000  
  52.     cursor.execute(sql_2)  # 支出增加1000  
  53.     cursor.execute(sql_3)  # 收入增加2000  
  54. except Exception as e:  
  55.     connect.rollback()  # 事务回滚  
  56.     print(‘事务处理失败‘, e)  
  57. else:  
  58.     connect.commit()  # 事务提交  
  59.     print(‘事务处理成功‘, cursor.rowcount)  
  60.   
  61. # 关闭连接  
  62. cursor.close()  
  63. connect.close()  
结果如下:

附加MySQL相关知识:

如何用MySQL建立数据库

启动mysql
查看现有数据库
mysql> show databases;

创建数据库(假如数据库名为 mydb)
mysql> create database mydb;

删除数据库(假如数据库名为 mydb)
mysql> drop database accounts;

使用数据库(假如使用数据库 mydb)
mysql> use mydb;
执行完使用数据库命令后,就可以对该数据库进行创建、修改、插入、删除表等操作,

mysql数据库怎么创建数据表并添加数据

使用 create table 语句可完成对表的创建, create table 的常见形式:
create table 表名称(列声明);
以创建 students 表为例, 表中将存放 学号(id)、姓名(name)、性别(sex)、年龄(age)、联系电话(tel) 这些内容:
create table students

id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);

向表中插入数据
insert 语句可以用来将一行或多行数据插到数据库表中, 使用的一般形式如下:
insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);
其中 [] 内的内容是可选的, 例如, 要给 samp_db 数据库中的 students 表插入一条记录, 执行语句:
insert into students values(NULL, "王刚", "男", 20, "13811371377");
按回车键确认后若提示 Query Ok, 1 row affected (0.05 sec) 表示数据插入成功。 若插入失败请检查是否已选择需要操作的数据库。

有时我们只需要插入部分数据, 或者不按照列的顺序进行插入, 可以使用这样的形式进行插入:
insert into students (name, sex, age) values("孙丽华", "女", 21);

 

python3.6 与MYSQL的安装与连接

标签:line   .exe   多行   use   bug   mysql安装   print   creat   select   

人气教程排行