最近用到Mysql,操作相关的记录如下
系统:Windows 软件相关:Python+mysqldb
安装MySQL:rj.baidu.com/soft/detail/12585.html
安装Python:略
安装mysqldb:blog.csdn.net/wklken/article/details/7253245
cmd下的操作:
1、登录:>> mysql -u root -p密码
2、展示: >>show databases;
>>show tables;
>>describe 列表名;
3、创建库:>>create database 库名;
>> create database if not exists 库名;
4、选择库:>>use 库名
5、创建表:>>create table 表名(
id int(1) unsigned not null auto_increment primary key,
data_id int(1),
content text(1000),
time_a datatime
);
>>create table if not exists 表名(同上);
6、检索全部表:SELECT * FROM 表名;
检索特定行:select *from 表名 where id=2;
检索特定字段:select id from 表名;
7、更新指定信息:UPDATE 表名 SET content = hello china WHERE id = 2;
8、按关键词检索:SELECT * FROM budejie_db.fristpage WHERE budejie_db.fristpage.id = 2017 OR budejie_db.fristpage.data_id= 2017
OR CAST(budejie_db.fristpage.content AS CHAR CHARACTER SET utf8) COLLATE utf8_general_ci LIKE '%2017%'
OR CAST(budejie_db.fristpage.time AS CHAR CHARACTER SET utf8) COLLATE utf8_general_ci LIKE '%2017%';
其中:2017是关键词,%是(0~n个)通配字符,_是1个通配字符。
8、其他:www.cnblogs.com/bzys/archive/2013/01/20/2869029.html
Python下的操作:
import MySQLdb
db = MySQLdb.connect("localhost","root","560320",port=3306,charset="utf8")
cursor = db.cursor()#cursor()获取操作游标
cursor.execute("create database if not exists budejie_db")#创建数据库
db.select_db('budejie_db')#选中数据库
cursor.execute("select version()")#execute()方法执行sql语句
#data = cursor.fetchone()#fetchone()方法获取操作结果
#print "数据库版本:%s" % data
#cursor.execute("drop table if exists contents")#删除数据表
#创建数据表
sql = "create table if not exists test(" \
"id int(1) unsigned not null auto_increment primary key," \
"data_id int(1)," \
"content text(1000)" \
");"
cursor.execute(sql)
#-------------------测试------------
cursor.execute('insert into test values(NULL,224,"iie");')
cursor.execute('insert into test values(NULL,818,"feiw");')
db.commit()
cursor.close()
db.close()
网友评论