python *** 作数据库mysql数据库出现错误奇怪?望高人指点?

python *** 作数据库mysql数据库出现错误奇怪?望高人指点?,第1张

给你我写的python mysql扩展吧

#-*- coding: utf-8 -*-

#mysql by final v1.03e

from warnings import filterwarnings

import MySQLdb

filterwarnings('ignore', category = MySQLdb.Warning)

#from warnings import resetwarnings

#resetwarnings()

class mysql:

def __init__(self,host,user,passwd,db,timeout=5,language="utf8"): 

self.host=host

self.user=user

self.passwd=passwd

self.db=db

self.sql=""

self.timeout=timeout

try:

self.conn=MySQLdb.connect(host=self.host,user=self.user,passwd=self.passwd,db=self.db)#,connect_timeout=self.timeout)

except:

print "MySQLdb connect error"

return

else:

pass

self.language(language)

self.cursor=self.conn.cursor(MySQLdb.cursors.DictCursor)

#self.cursor=self.conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)

self.cursor.execute("set names '%s'"%language)

self.conn.text_factory=str

self.fieldsarr=[]

self.pagesize=20

self.page=0

self.usepage=0

def language(self,language):

self.conn.set_character_set(language)

def fields(self):

return self.fieldsarr

def setpagesize(self,pagesize=0): 

         self.pagesize=pagesize

def getpageinfo(self):

ifre=re.search("select ([0-9a-zA-Z|,| |']*) from ([0-9a-zA-Z|,|'|_| ]*)",self.pagesql)

maxcount=self.fetch("select count(*) from %s"%ifre.group(2))[0]

return [maxcount,int(maxcount/self.pagesize)]

def query(self,sql):

#print "!3"

if self.usepage==1:

sql="%s limit %s,%s"%(sql,(self.page-1)*self.pagesize,self.pagesize)

self.usepage=0

self.sql=sql

#print sql

self.cursor.execute(sql)

def execute(self,sql):

self.sql=sql

self.cursor.execute(sql)

self.conn.commit()

def fetchall(self,sql="",page=-1):

if page>=0:

self.page=page+1

self.usepage=1

self.pagesql=sql

if sql:

self.query(sql)

#self.fieldsarr=["%s"%x[0] for x in self.cursor.description]

self.fieldsarr=self.cursor.description

return self.cursor.fetchall()

def fetch(self,sql=""):

#print "222"

if sql:

self.query(sql)

#self.fieldsarr=["%s"%x[0] for x in self.cursor.description]

#print 0

self.fieldsarr=self.cursor.description

#print 1

mfetchall=self.cursor.fetchall()

#print 3

if mfetchall:

return mfetchall[0]

else:

return []

def convstr(self,s,fun=None):

if type(s) in (type(u""),type("")):

if fun:

#print "k fun"

return fun(s,139)

else:

#print "no str"

return str(s).replace("'","''")

else:

#print "num"

return s

def tosql(self,sql):

return self.conn.escape_string(sql)

def addnew(self,tablename,data={},fun=None,commit=True):

if fun:

sql="insert into %s (%s) values (%s)"%(tablename,",".join(["`%s`"%y for y in data]),",".join(["'%s'"%self.convstr(data[y],fun) for y in data]))

else:

sql="insert into %s (%s) values (%s)"%(tablename,",".join(["`%s`"%y for y in data]),",".join(["'%s'"%str(data[y]).replace("'","''") for y in data]))

sql=sql.replace("'fdm:datetime'","now()")

sql=sql.replace("'fdm:time'","UNIX_TIMESTAMP()")

#print sql

self.cursor.execute(sql)

self.insertid=int(self.conn.insert_id())

if commit==True:

self.conn.commit()

def delete(self,tablename,ids=[]):

sql="delete from %s where id in (%s)"%(tablename,",".join(["%s"%y for y in ids]))

self.cursor.execute(sql)

self.conn.commit()

def update(self,tablename,data={},where=""):

sql="update %s set %s"%(tablename,",".join(["`%s`='%s'"%(x,str(data[x]).replace("'","''")) for x in data]))

if where:

sql="%s where %s"%(sql,where)

sql=sql.replace("'fdm:datetime'","now()")

sql=sql.replace("'fdm:time'","UNIX_TIMESTAMP()")

#print sql

self.cursor.execute(sql)

self.conn.commit()

def __del__(self):

print "close mysql"

self.cursor.close()

self.conn.close()

def insert_id(self):

self.conn.insert_id()

def close(self):

self.__del__()

不要刚开始学多线程编程就这样玩。connection 和 cursor 都不是线程安全的。

如果测试环境用多个线程,每个线程要在线程里面获取自己的 connection,然后从这个connection 获取 cursor.

如果生产环境用多个线程,建议使用线程安全的连接池。

Python连接mysql数据库报错

这里的意思是:数据库连不上啊。

可能是网络问题,可能是防火墙问题,可能是3306端口没开。你先排除这些问题吧。用一些mysql工具连接测试看,比如SQLyog 测试。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6684159.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-26
下一篇2023-03-26

发表评论

登录后才能评论

评论列表(0条)

    保存