一、首先安装mysql数据库

准备材料:mysql源码包

1、安装rpm包:  ncurses-devel   gcc-c++

2、创建data目录: 

  

3、创建用户及组并将数据目录修改属主属组:

4、将cmake解包并执行:

5、将mysql解包并执行:

cmake 

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql

-DMYSQL_DATADIR=/data/mysql/mysql

-DDEFUALT_CHARSET=utf8

-DEXTRA_CHARSETS=all

-DDEFAULT_COLLATION=utf8_general_ci

-DMYSQL_UNIXADDR=/tmp/mysql.sock

-DWITH_INNOBASE_STORAGE_ENGINE=1

-DWITH_ARCHIVE_STORAGE_ENGIN=1

-DWITH_BLACKHOLE_STORAGE_ENGINE=1

-DWITH_PERFSCHEMA_STORAGE_ENGINE=1

-DWITH_MYISAM_STORAGE_ENGINE=1

-DENABLE_LOCAL_INFILE=1

-DENABLE_DTRACE=0

-DMYSQL_USER=mysql

-DMYSQL_TCP_PORT=3306

-DWITH_EMBEDDED_SERVER=1

然后make  &&  make  install

如果安装不成功,要清空配置:make  clean  all

6、安装成功后再:

7、然后在/etc/profile里加入:

 

7、然后初始化:

 

初始化后若启动失败,可在初始化时候再加上: --ldata=/var/lib/mysql   再试一下

8、启动报错问题:

 

此问题重新初始化下数据库:

 

再重启,可以正常启动。

更多:http://www.cnblogs.com/wupeiqi/articles/5699254.html

二、安装API   pymysql

pip3 install pymysql即可

pymysql的使用:

import pymysql

# 创建连接
conn = pymysql.connect(host='192.168.254.129', port=3306, user='test',passwd='123456',db='test')
# 创建游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
print(cursor)
# 执行sql命令,并返回受影响的行数
# ret = cursor.execute("insert into sanguo VALUES ('zhugeliang',21,'shu',4)")
# 可以使用百分号
# ret = cursor.execute("select * from sanguo WHERE id > %s" %2)
# 批量修改
# ret = cursor.executemany("insert into sanguo (name,age) VALUES (%s,%s)", [('xunyu',23),('zhangfei',22)])
ret = cursor.execute("select name,age from sanguo where id>3")
print(ret)
# 获取第一行数据
# row_1 = cursor.fetchone()
# 获取指定多行数据
# row_2 = cursor.fetchmany(2)
# 获取全部数据
row_3 = cursor.fetchall()
print(row_3)
# 提交,不然无法保存修改
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
# 获取最新自增id,即执行以上sql时,获取到的新的第一个id,如果sql没有使id增加,则返回None
new_id = cursor.lastrowid
print(new_id)
import pymysql

# 创建连接
conn = pymysql.connect(host='192.168.254.129', port=3306, user='test',passwd='123456',db='test')
# 创建游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
print(cursor)
# 执行sql命令,并返回受影响的行数
# ret = cursor.execute("insert into sanguo VALUES ('zhugeliang',21,'shu',4)")
# 可以使用百分号
# ret = cursor.execute("select * from sanguo WHERE id > %s" %2)
# 批量修改
# ret = cursor.executemany("insert into sanguo (name,age) VALUES (%s,%s)", [('xunyu',23),('zhangfei',22)])
ret = cursor.execute("select name,age from sanguo where id>3")
print(ret)
# 获取第一行数据
# row_1 = cursor.fetchone()
# 获取指定多行数据
# row_2 = cursor.fetchmany(2)
# 获取全部数据
row_3 = cursor.fetchall()
print(row_3)
# 提交,不然无法保存修改
conn.commit()
# 此时的游标在最后,游标向前移动1
# cursor.scroll(-1,mode='relative')
# print(cursor.fetchall())
# 此时游标在最后,游标移动到1位置
# cursor.scroll(1,mode='absolute')
# print(cursor.fetchall())
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
# 获取最新自增id,即执行以上sql时,获取到的新的第一个id,如果sql没有使id增加,则返回None
new_id = cursor.lastrowid
print(new_id)
示例

 

PS:fetch默认获取的数据是元祖类型,如果想要字典类型的数据,可cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:
 cursor.scroll(1,mode='relative')  # 相对当前位置移动

 cursor.scroll(2,mode='absolute') # 相对绝对位置移动

 

posted on 2017-05-16 15:01  自然洒脱  阅读(181)  评论(0编辑  收藏  举报