1.打开终端输入 service mariadb start,启动服务器
2.mysql -u root -p enter 键 连接mysql
3.操作数据库
show databases;
show tables;
CREATE TABLE user(name VARCHAR(20),passward VARCHAR(20)); 创建user表
insert into user values('tom','1245');
select *from user;查看user表
delect from user where name='Jack';
updata user set passward='1111' where name='Alen';更新数据库
安装MySQLdb-python
搜索--sudo yum mysql|grep python
安装--sudo yum install ......
mysql-python操作
#coding=utf-8 import MySQLdb conn=MySQLdb.Connect( host='localhost', port=3306, user='root', passwar='', db='test', ) cur=conn.cursor()#连接数据库以后,必须建立游标cur '''cur.execute("create table student(id int,name varchar(20),class varchar(30),age varchar(10))") cur.execute("insert into student values('2','Tom','3 year 2 class','9')") cur.execute("updatemx student set class='3 year 1 class' where name='Tom'") cur.execute("delete from student where age='9'") ''' sqli="insert into student values(%s,%s,%s,%s)" cur.executemany(sqli,[('3','tom','1 year 2 class','6'),(),()])#输入多行 cur.fetchmany()#打印多行 cur.close() conn.autocommit()#必须commit,提交才有效 conn.close()
更改后
#coding=utf-8 import MySQLdb conn=MySQLdb.Connect( host='localhost', port=3306, user='root', passwd='', db='test', ) cur=conn.cursor() #cur.execute("create table student(id int,name varchar(20),class varchar(30),age varchar(10))") cur.execute("insert into student values('2','Tom','3 year 2 class','9')") cur.execute("update student set class='3 year 1 class' where name='Tom'") conn.commit() cur.execute("select *from student") print cur.fetchmany()#打印多行 #增删改查以后先commit #cur.execute("delete from student where age='9'") #cur.execute("drop table student") ''' sqli="insert into student values(%s,%s,%s,%s)" cur.executemany(sqli,[('3','tom','1 year 2 class','6'),(),()])#输入多行 ''' cur.close() #conn.commit() conn.close()正确打印结果:
/usr/bin/python2.7 /home/zhangyanan/PycharmProjects/startpython/start3.py
((2L, 'Tom', '3 year 1 class', '9'),)
Process finished with exit code 0