[英]SQL inside code or procedure?
This is a very straight forward question regarding how to insert or select data from/to a database ? 关于如何从数据库中插入数据或从中选择数据,这是一个非常直接的问题? Since i'm trying to keep my code as clean as possible, this is how i'm actually performing queries and inserts/updates:
由于我试图保持代码尽可能整洁,因此这是我实际执行查询和插入/更新的方式:
import sys
import MySQLdb
from ConfigParser import SafeConfigParser
#------------------------------------------------------------
# Select and insert
# this func should be called like:
# db_call('c:\dbconf.cfg','select * from something')
# or insert / update statement.
#------------------------------------------------------------
def db_call(cfgFile, sql):
parser = SafeConfigParser()
parser.read(cfgFile)
dbType = parser.get('database', 'db_type')
db_host = parser.get('database', 'db_host')
db_name = parser.get('database', 'db_name')
db_user = parser.get('database', 'db_login')
db_pass = parser.get('database', 'db_pass')
con = MySQLdb.connect(host=db_host, db=db_name,
user=db_user, passwd=db_pass
)
cur = con.cursor()
try:
try:
cur.execute(sql)
if re.match(r'INSERT|insert|UPDATE|update|DELETE|delete', sql):
con.commit()
else:
data = cur.fetchall()
resultList = []
for data_out in data:
resultList.append(data_out)
return resultList
except MySQLdb.Error, e:
con.rollback()
print "Error "
print e.args
sys.exit(1)
else:
con.commit()
finally:
con.close()
But, using this method i have to keep all the queries inside my main class, where that can be a problem if any change happens into the table structure, 但是,使用这种方法,我必须将所有查询保留在我的主类中,如果表结构发生任何变化,那可能是一个问题,
But, going for sp call, i can have the code more clean, passing only the sp name and fields. 但是,对于sp调用,我可以使代码更整洁,仅传递sp名称和字段。 But sometimes this could lead me to have one python function for more specific cases, ( as an example, sp that receives 2,3 or 4 inputs must have diferent python functions for each )
但这有时可能会使我在更特定的情况下拥有一个python函数(例如,接收2,3或4个输入的sp必须为每个函数具有不同的python函数)
import sys
import MySQLdb
from ConfigParser import SafeConfigParser
#------------------------------------------------------------
# Select only!!!!!!
# this func should be called like:
# db_call('fn_your_function','field_a','field_b')
# or insert / update statement.
#------------------------------------------------------------
def db_call(self, cfgFile, query):
parser = SafeConfigParser()
parser.read(cfgFile)
dbType = parser.get('database', 'db_type')
db_host = parser.get('database', 'db_host')
db_name = parser.get('database', 'db_name')
db_user = parser.get('database', 'db_login')
db_pass = parser.get('database', 'db_pass')
con = MySQLdb.connect(host=db_host, db=db_name,
user=db_user, passwd=db_pass
)
cur = con.cursor()
try:
cur.callproc(query[0], (query[1],query[2]))
data = cur.fetchall()
resultList = []
for data_out in data:
resultList.append(data_out)
return resultList
con.close()
except MySQLdb.Error, e:
con.rollback()
print "Error "
print e.args
sys.exit(1)
Im not sure if here is the right place to ask this, but before voting to close it (if is the case ) please reply with the information where i could ask this kind of question :) 我不确定这里是否是问这个问题的合适地点,但是在投票关闭它之前(如果是这样),请在可以提出此类问题的信息中答复:)
Thanks in advance. 提前致谢。
If your goal is to abstract away the schema of your DB from your objects' implementations, you should probably be looking at ORMs/persistence frameworks. 如果您的目标是从对象的实现中抽象出数据库的架构,则您可能应该研究ORM /持久性框架。 There are a number of them in Python.
Python中有很多。 As examples, SQLAlchemy is popular and Django, a popular web framework, has one built in.
例如,SQLAlchemy很流行,而Django是一个流行的Web框架,内置了一个框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.