1樓:玩轉資料處理
#port = '3306' 錯誤,改為 port = 3306#!/usr/bin/env python#coding=utf-8
import mysqldb
conn = mysqldb.connect(host = '192.188.
80.41',port = 3306,user = 'root',passwd = 'root',db = 'mysql237_hdr',charset='utf8')
cur = conn.cursor()
#查詢count = cur.execute("select count(*) from hdr_analoginput20140530")
print 'total analoginput is %d' %count
#關閉cur.close()
conn.close()
2樓:
#!/usr/bin/env python#coding=utf-8
import mysqldb
conn = mysqldb.connect(host = '192.188.80.41',port = 3306,
user = 'root',
passwd = 'root',
db = 'mysql237_hdr',
charset='utf8')
cur = conn.cursor()
cur.close()
conn.close()
python運算元據庫mysql資料庫出現錯誤奇怪?望高人指點?
3樓:
給你我寫的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__()
python使用open命令開啟檔案失敗
確認下bai你的1.txt 到底在哪兒?duwindows環境下 是用反斜 zhi槓 open c xx1 xx2 x.txt open r c xx1 xx2 x.txt 你的第四次語法是 dao沒問題內的,報的是檔案不存在 如果不指容定目錄 open 1.txt 會到執行這條語句的當前目錄下找你...
如何使用Python自帶編輯器IDLE
idle的除錯方法 1.設定斷點 在python編輯器中要除錯的 行右擊 set breakpoint,之後該行底色 2.開啟debugger python shell debug debugger 3.編輯視窗按f5 4.debug過程略 go表示執行完相當於eclipse的f8,不過按f5後先要...
Python字串是什麼,如何使用?
python的字串就是表示一串字元,字元可以是中文,英文或者數字,或者混合的文字。python字串的幾種表達方式。1 使用單引號擴起來字串。2 使用雙引號將字串擴起來。3 當想要輸出單引號或者雙引號時 將單引號,雙引號作為普通字元輸出 通過 進行轉義。4 通過單引號,雙引號混合的方式輸出單引號,雙引...