2014-12-19 149 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