2017-02-21 53 views
0

我一直在嘗試創建一個時鐘進出系統,爲我工作的小企業。Python MYSQL問題

目前我無法與MySQL服務器通話。我可能在做一些非常愚蠢的事情。

我有困難的部分是這樣的:

cnx = mysql.connector.connect(host=localhost, user="time_clock", passwd="kayak100", db="Staff_time_clock") 
cursor = cnx.cursor() 
staffid = input('Please enter your Staff ID now ...') 
idcheck = ("SELECT staff_name, staff_id FROM Staff WHERE staff_id = %s") 
cursor.execute(idcheck,(staffid)) 

我很新的使用MYSQL,並學會了嘗試做這個小程序。

編輯 - 是的抱歉忘了添加我得到的錯誤代碼。

--------------------------------------------------------------------------- 
ProgrammingError       Traceback (most recent call last) 
<ipython-input-49-b672a17a258e> in <module>() 
----> 1 cursor.execute(idcheck,) 

C:\Users\ellio\Anaconda3\lib\site-packages\mysql\connector\cursor.py in  execute(self, operation, params, multi) 
    505    self._executed = stmt 
    506    try: 
--> 507      self._handle_result(self._connection.cmd_query(stmt)) 
    508    except errors.InterfaceError: 
    509     if self._connection._have_next_result: # pylint: disable=W0212 

C:\Users\ellio\Anaconda3\lib\site-packages\mysql\connector\connection.py in cmd_query(self, query) 
    720   if not isinstance(query, bytes): 
    721    query = query.encode('utf-8') 
--> 722   result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query)) 
    723 
    724   if self._have_next_result: 

C:\Users\ellio\Anaconda3\lib\site-packages\mysql\connector\connection.py in _handle_result(self, packet) 
    638    return self._handle_eof(packet) 
    639   elif packet[4] == 255: 
--> 640    raise errors.get_exception(packet) 
    641 
    642   # We have a text result set 

ProgrammingError: 1064 (42000): 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 '%s' at line 1 

這是總的錯誤。

謝謝

+1

什麼是你有確切的問題? –

+0

添加你得到的輸出(錯誤) – davejal

回答

0

你的問題是,你沒有正確格式化你的字符串。

嘗試運行以下命令:

cnx = mysql.connector.connect(host=localhost, user="time_clock", passwd="kayak100", db="Staff_time_clock") 
cursor = cnx.cursor() 
staffid = input('Please enter your Staff ID now ...') 
idcheck = "SELECT staff_name, staff_id FROM Staff WHERE staff_id = %s" % staffid 
cursor.execute(idcheck) 
+0

謝謝@AvihooMamka已經解決了它。在網上查了很多,但有太多的變化,似乎沒有工作。 –

+0

你可以進一步看看字符串格式[這裏](https://pyformat.info/) –