python利用MySQLdb模块操作mysql数据库,创建schema 和table 然后是打开数据库显示表结构等操作:
#!/usr/bin/python# coding=utf8import MySQLdbdb = MySQLdb.connect(host='localhost', user='root', passwd='*******')cursor = db.cursor()#db.select_db("log_Analysis")init_db = """create database if not exists log_Analysis DEFAULT CHARSET utf8 COLLATE utf8_general_ci"""cursor.execute(init_db)db.select_db("log_Analysis")init_sql = """CREATE TABLE IF NOT EXISTS record ( analysis_time Datetime, id int (10) NOT NULL, album_id int (10) NOT NULL, PRIMARY KEY(album_id), album_name char (20) NOT NULL, customerId int (20) NOT NULL, clicks int (20) NOT NULL, client_ip char (20) NOT NULL)"""cursor.execute(init_sql)db.close()#Open database connectiondb = MySQLdb.connect(host='localhost',user='root',passwd='*******')# prepare a cursor object using cursor() methodcursor = db.cursor()#select schemadb.select_db("log_Analysis")#execute sqlcursor.execute("desc record")result = cursor.fetchall()#print outputfor i in result: print(i)db.close()
python create mysql db table,首发于运维者。
最快乐的时候,就是去旅行。