python 使用两个脚本进行数据库查询
python database query using two scripts
我有两个脚本 main.py 和 testscript.py。在 main.py 上是连接到我的数据库的功能,在 testscript.py 上我将用作我的查询请求并使用 main.py 将此查询调用到我的数据库。添加到 main.py
if __name__ == '__main__':
q = "select domain_name from domains_domain limit 10"
query(q)
并运行脚本
python main.py
按预期工作,但我不知道如何在 testscript.py
上执行此操作
这里是main.py
import MySQLdb
from localdb import *
import socket
host = socket.gethostname()
error_report = "Check if MySQL service is running and user name/password are correct"
class WeekQuery:
def __init__(self, name):
self.name = name
def query(self):
cursor = None
results = None
week_number = None
#Connecting to production database
if host == 'db01':
try:
db = MySQLdb.connect("db01", "myuser", "mypass", "mydb")
cursor = db.cursor()
cursor.callproc(self)
results = cursor.fetchall()
week_number = [i[0] for i in cursor.description]
except MySQLdb.Error, e:
try:
print "%s \n MySQL Error [%d]: %s" % (error_report, e.args[0], e.args[1])
except IndexError:
print "%s \n MySQL Error: %s " % (error_report, str(e))
else:
#local database rename localdb.py_example to localdb.py
try:
db = MySQLdb.connect(host=MYSQL_HOST, user=MYSQL_USER, passwd=MYSQL_PASSWD, db=MYSQL_DATABASE)
cursor = db.cursor()
cursor.execute(self)
results = cursor.fetchall()
week_number = [i[0] for i in cursor.description]
except MySQLdb.Error, e:
try:
print "%s \n MySQL Error [%d]: %s" % (error_report, e.args[0], e.args[1])
except IndexError:
print "%s \n MySQL Error: %s " % (error_report, str(e))
#finally:
# cursor.close()
# db.close()
print(results, week_number) #For debuging only
return (results, week_number)
这是我的testscript.py
from main import WeekQuery
def sample():
q = "select domain_name from domains_domain limit 10"
x = WeekQuery(q)
for y in x.query():
print(y)
您还需要从 main 中导入主机和 error_report。这是你的问题吗?
我有两个脚本 main.py 和 testscript.py。在 main.py 上是连接到我的数据库的功能,在 testscript.py 上我将用作我的查询请求并使用 main.py 将此查询调用到我的数据库。添加到 main.py
if __name__ == '__main__':
q = "select domain_name from domains_domain limit 10"
query(q)
并运行脚本
python main.py
按预期工作,但我不知道如何在 testscript.py
上执行此操作这里是main.py
import MySQLdb
from localdb import *
import socket
host = socket.gethostname()
error_report = "Check if MySQL service is running and user name/password are correct"
class WeekQuery:
def __init__(self, name):
self.name = name
def query(self):
cursor = None
results = None
week_number = None
#Connecting to production database
if host == 'db01':
try:
db = MySQLdb.connect("db01", "myuser", "mypass", "mydb")
cursor = db.cursor()
cursor.callproc(self)
results = cursor.fetchall()
week_number = [i[0] for i in cursor.description]
except MySQLdb.Error, e:
try:
print "%s \n MySQL Error [%d]: %s" % (error_report, e.args[0], e.args[1])
except IndexError:
print "%s \n MySQL Error: %s " % (error_report, str(e))
else:
#local database rename localdb.py_example to localdb.py
try:
db = MySQLdb.connect(host=MYSQL_HOST, user=MYSQL_USER, passwd=MYSQL_PASSWD, db=MYSQL_DATABASE)
cursor = db.cursor()
cursor.execute(self)
results = cursor.fetchall()
week_number = [i[0] for i in cursor.description]
except MySQLdb.Error, e:
try:
print "%s \n MySQL Error [%d]: %s" % (error_report, e.args[0], e.args[1])
except IndexError:
print "%s \n MySQL Error: %s " % (error_report, str(e))
#finally:
# cursor.close()
# db.close()
print(results, week_number) #For debuging only
return (results, week_number)
这是我的testscript.py
from main import WeekQuery
def sample():
q = "select domain_name from domains_domain limit 10"
x = WeekQuery(q)
for y in x.query():
print(y)
您还需要从 main 中导入主机和 error_report。这是你的问题吗?