美文网首页我爱编程
2018-04-07 MySQL与Python交互

2018-04-07 MySQL与Python交互

作者: 北漠_a59e | 来源:发表于2018-04-07 20:23 被阅读0次

与Python交互需要先安装pymysql

代码为:

sudo    pip3    install    pymysql

连接数据库:

import    pymysql

打开数据库连接:

db = pymysql.connect("MySQL地址","账号","密码","数据库名称")

使用cursor创建一个游标对象:

cursor = db.cursor()

使用execute()方法执行SQL查询:

cursor.execute("SHOW DATABASES")

单挑语句:

data    =     cursor.fetchone()

输出:

print("Database version :%s"%data)

关闭数据库:

db.close()

自关联语句:

create tablebooktest_areas(

id int primary key,

atitle varchar(20),

pid int,

foreign key(pid) references areas(id)

);

从sql文件中导入数据:

source    areas.sql;

事务四大特性(简称ACID)

原子性

一致性

隔离性

持久性   

要求表的类型必须是innodb或bdb类型,才可以对此表使用事务

修改表的类型:

alter    table    "表名"    engine=innodb;

事务语句

开启:begin;

提交:commit;

回滚:rollback;

视图本质就是对查询的一个封装

定义视图

create view stuscore as

select students.*,scores.score from scores

inner join students on scores.stuid=students.id;

视图的用途就是查询

select * from stuscore;

相关文章

网友评论

    本文标题:2018-04-07 MySQL与Python交互

    本文链接:https://www.haomeiwen.com/subject/zszyhftx.html