2014-12-19 144 views
-1

我有這樣的選擇查詢:用mysql選擇查詢

SELECT * FROM simple_tbl WHERE a_name != " " AND d_name != " " AND s_time != "0000-00-00 00:00:00" AND e_time != "0000-00-00 00:00:00"

我有一個python腳本使用此,我用選擇cursor.execute(「查詢」內),但每次都失敗。

任何人都可以幫助我使用它嗎?

我用以下2種方式:

1: cursor.execute ("SELECT * FROM maintenance_simple_tbl WHERE acc_name != " " AND device_name != " " AND start_time != '0000-00-00 00:00:00' AND end_time != '0000-00-00 00:00:00'")

錯誤:

Traceback (most recent call last): File "sync.py", line 42, in <module> cursor.execute ("SELECT * FROM simple_tbl WHERE a_name != " " AND d_name != " " AND s_time != '0000-00-00 00:00:00' AND e_time != '0000-00-00 00:00:00'") File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute self.errorhandler(self, exc, value) File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND d_name != ND s_time != '0000-00-00 00:00:00' AND e_time != '000' at line 1")

第二: cursor.execute ("SELECT * FROM simple_tbl WHERE a_name != " " AND d_name != " " AND start_time !='%s' AND e_time !='%s'" % (0000-00-00 00:00:00, 0000-00-00 00:00:00))

錯誤:

File "sync.py", line 43 cursor.execute ("SELECT * FROM simple_tbl WHERE a_name != " " AND d_name != " " AND s_time !='%s' AND e_time !='%s'" % (0000-00-00 00:00:00, 0000-00-00 00:00:00)) ^ SyntaxError: invalid syntax

+0

你能更具體地說明你如何使用這個查詢嗎?向我們展示一些代碼,以便我們可以幫助您發現問題... – Oscar 2014-12-19 10:29:40

+0

您是否可以包含錯誤('traceback'),並且還包含您的問題中的Python代碼。 – 2014-12-19 10:31:23

+0

您是否在沒有te腳本的情況下在數據庫上嘗試此查詢?您是否100%確定查詢是正確的? – 2014-12-19 10:47:13

回答

1

這裏是你如何進行查詢的MySQL在Python:

import MySQLdb as sql 

dbParams = { 
    'host': # your database host, 
    'port': # your database port, 
    'user': # your database user, 
    'passwd': # your database password, 
    'db': # your database name 
} 
query = 'SELECT * FROM simple_tbl WHERE a_name != " " AND d_name != " " AND s_time != "0000-00-00 00:00:00" AND e_time != "0000-00-00 00:00:00"' 

database = sql.connect(**dbParams) # initiate a connection to your database 
cursor = database.cursor() # create a new cursor that you will use to query your database 
cursor.execute(query) # this will execute your query 
result = cursor.fetchall() 

而不是fetchall()功能,你可以只使用fetchone()如果你要檢索的第一個返回的行。

+0

謝謝。我能夠連接並獲取數據。我的主要問題是在腳本中正確引用引號。 – kumarprd 2014-12-19 11:26:35

1

儘管其他答案可以用於執行查詢和獲取結果,但我認爲DictCursor對象更符合您在處理MySQL表時所需的內容。您可以按如下方式使用它:

import MySQLdb 
import MySQLdb.cursors 

def get_db_cursor(): 
    db = MySQLdb.connect(host="host", 
         user="user", 
         passwd="passwd", 
         db="db_name", 
         cursorclass=MySQLdb.cursors.DictCursor) 
    cur = db.cursor() 
    return cur 

現在,你可以使用這個功能如下獲得光標:

cursor = get_db_cursor() 
sql = "SELECT * simple_tbl WHERE a_name != ' ' AND d_name != ' ' AND s_time != '0000-00-00 00:00:00' AND e_time != '0000-00-00 00:00:00' " # Note that I replaced the double quotes with single quotes 
cursor.execute(sql) 
data = cursor.fetchall() 

這將返回字典的集合 - 在結果表中每行一個。你可以如下遍歷這個集合:

for row in data: 
    if len(row['a_name']) > 5: 
     # do something