MariaDB是mysql的一个分支,也是一个开源数据库,它的存在防止mysql闭源,就是那么奇葩。除了具有Mysql数据库系统的特点外,还是具有自身的一些特性:
兼容性 : mysql能用的,mariadb也能用速度更快:它在安全复制、索引、字符集转换等方面具有更好的性能。采用线程池连接查询:并发防止高用户量死机安全性:有一套安全补丁.好用实在。MariDB安装路径 详细教大家如何安装maridb,然后我们可以运用python的第三方库mysql connector python来操作mariadb数据库和本书前一节中操作SQLite3基本相同,其步骤相似。 mysql-connector-python模块中的连接函数connect()的包路径为mysql.connector.connect,其函数原型如下:
connect(host,port,user,password,database,charset)各参数含义如下:
host 访问数据库的服务器主机(默认为本机)port 访问数据库的服务端口(默认为3306)user 访问数据库的用户名password 访问数据库用户名的密码database 访问数据库名称charset 字符编码效果 实验代码:
from mysql import connector import random src = 'abcdefghijklmnopqrstuvwxyz' def get_data_list(n): res = [] for i in range(n): res.append((get_str(2,4),get_str(8,12))) return res def output(): cur.execute('select * from mytab') for sid,name,ps in cur: print(sid,' ',name,' ',ps) def output_all(): cur.execute('select * from mytab') for item in cur.fetchall(): print(item) def get_str(param, param1): str_num = random.randint(param,param1) astr = '' for i in range(str_num): astr += random.choice(src) return astr if __name__ == '__main__': print('建立连接...') con = connector.connect(user='root',password='123456',database = 'test') print('建立游标...') cur = con.cursor() print('创建一张表mytab...') cur.execute('create table mytab(id int primary key auto_increment not null,name text,passwd text)') print('插入一条记录...') cur.execute('insert into mytab (name,passwd) values(%s,%s)',(get_str(2,4),get_str(8,12),)) print('显示所有记录...') output() print('批量插入多条记录') cur.executemany('insert into mytab (name,passwd) values(%s,%s)',get_data_list(3)) print('显示所有记录...') output_all() print('更新一条记录...') cur.execute('update mytab set name=%s where id = %s',('aaa',1)) print('显示所有记录....') output() print('删除一条记录...') cur.execute('delete from mytab where id=%s',(3,)) print('显示所有记录:') output() cur.close() con.close()