读取表中的内容,如下例子:
import MySQLdb try: conn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘root‘,db=‘mydb‘,port=3306) df = pd.read_sql(‘select * from test;‘, con=conn) conn.close() print "finish load db" except MySQLdb.Error,e: print e.args[1]
写入数据到表中,如下例子
df = pd.DataFrame([[1,‘xxx‘],[2,‘yyy‘]],columns=list(‘ab‘)) try: conn = MySQLdb.connect(host=‘127.0.0.1‘,user=‘root‘,passwd=‘root‘,db=‘mydb‘,port=3306) df.to_sql(‘test1‘,conn,flavor=‘mysql‘) conn.close() print "finish load db" except MySQLdb.Error,e: print e.args[1]
说明:上面代码要求表test1不能存在
时间: 2024-10-21 11:19:21