linux下数据库使用

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
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值