mysql
下载网址,版本号database version 5.7.20
https://dev.mysql.com/downloads/file/?id=473309
w10 系统安装过程
http://m.blog.csdn.net/live4dream/article/details/78494243
安装过程错误部分从登陆进MySQL开始,重置初始密码命令为
ALTER USER 'ROOT'@‘localhost’IDENTIFIED BY '新密码’;
参考链接
http://m.blog.csdn.net/u010395948/article/details/50974993
》》》》》数据库链接实例获取数据库版本号及sql安装自带databases表
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import MySQLdb 2 def conn_connect(): 3 conn = MySQLdb.connect("localhost","root","123666","mysql")#连接sql数据库 4 cur = conn.cursor()#创建游标 5 cur.execute("SELECT VERSION()")#执行sql操作 6 #data = cur.fetchall()#把查询结果保存为序列的序列(元组) 7 data = cur.fetchone()#把查询结果保存为序列 8 print("database version %s"%data) 9 cur.execute("show databases") 10 databases = cur.fetchall() 11 print(databases) 12 cur.close() 13 conn.close() 14 15 conn_connect()
版本号返回结果
data = cur.fetchone()#把查询结果保存为序列
data = cur.fetchall()#把查询结果保存为序列的序列(元组)
》》》》增,删,改,查 数据库内容
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import MySQLdb 2 conn=MySQLdb.connect(host='localhost',port=3306,user='root',passwd='123666',db='test',charset='utf8') 3 cur = conn.cursor() 4 5 # 创建数据表 6 #cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))") 7 8 #插入一条数据 9 #cur.execute("insert into student values('2','Cero','3 year 2 class','12')") 10 11 12 #修改查询条件的数据 13 #cur.execute("update student set class='3 year 1 class' where name = 'Tom'") 14 15 #删除查询条件的数据 16 cur.execute("delete from student where age='9'") 17 18 cur.close() 19 conn.commit() 20 conn.close()
》》》》》函数方法创建一个表、
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import MySQLdb 2 def creat_table(): 3 #打开数据库连接 4 conn = MySQLdb.connect(host='localhost', port=3306, user='root', passwd='123666', db='test', charset='utf8') 5 cur = conn.cursor()#使用cursor()方法创建一个游标对象 6 cur.execute("drop table if exists student")#使用execute()方法执行sql,如果存在就删除 7 try: 8 sql = "create table student(id int ,name varchar(20),class varchar(30),age varchar(10))" 9 #预处理语句 10 cur.execute(sql)#使用execute()方法创建table 11 print("数据库创建成功") 12 except Exception as e: 13 print("创建失败%s"%e) 14 finally: 15 cur.close() 16 conn.commit() 17 conn.close() 18 creat_table()
》》》》》插入多条数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import MySQLdb 2 def Insert_table(): 3 #打开数据库连接 4 conn = MySQLdb.connect(host='localhost', port=3306, user='root', passwd='123666', db='test', charset='utf8') 5 cur = conn.cursor()#使用cursor()方法创建一个游标对象 6 insert_sql = "insert into student values(%s,%s,%s,%s)" 7 try: 8 cur.executemany(insert_sql, [ 9 ('3', 'Tim', '1 year 1 class', '5'), 10 ('3', 'Jack', '1 year 1 class', '6'), 11 ('3', 'cero', '3 year 2 class', '7'), 12 ]) 13 print("插入数据成功") 14 except Exception as e: 15 print("插入失败%s"%e) 16 finally: 17 cur.close() 18 conn.commit() 19 conn.close() 20 Insert_table()
》》》》》查询表中数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import MySQLdb 2 def Query_table(): 3 #打开数据库连接 4 conn = MySQLdb.connect(host='localhost', port=3306, user='root', passwd='123666', db='test', charset='utf8') 5 cur = conn.cursor()#使用cursor()方法创建一个游标对象 6 try: 7 query_sql = ("select * from student") 8 num = cur.execute(query_sql)#使用execute()方法获得表中的数据个数 9 print(num) 10 results = cur.fetchall() 11 for row in results: 12 f_id = row[0] 13 f_name = row[1] 14 f_class = row[2] 15 f_age = row[3] 16 print("id= %d,name = %s,class = %s,age = %d"%(int(f_id),f_name,f_class,int(f_age))) 17 # 打印表中的多少数据 18 # info = cur.fetchmany(num) 19 # for line in info: 20 # print(line) 21 print("查询数据成功") 22 except Exception as e: 23 print("查询失败%s"%e) 24 finally: 25 cur.close() 26 conn.commit() 27 conn.close() 28 Query_table()
》》》mySQl数据库中不能插入中文的处理办法
1. 修改MySQL安装目录下(C:\Program Files\MySQL\MySQL Server 5.5)的my.ini文件
设置: default-character-set=utf8
character-set-server=utf8
然后重启MySQL服务,MySQL 没有重启的命令,要先停止,然后再启动。开始 -> cmd -> net stop mysql -> net start mysql
2.设置表的编码格式,执行;alter table ttname CONVERT TO CHARACTER SET utf8;命令,其中ttname为表名.