如何使用其他数据库中的数据
你可以使用其他数据库中的数据。
首先,你需要指定表名和字段信息。可以按照以下方式操作:
在任务树中选择 “项目(Project)” 节点,点击页面右侧的 数据库(Database) 按钮。
设置数据库为手动模式,并填写数据库连接的属性。
按照 集成已有数据库 的说明导入表信息。
在任务树中再次选择 “项目(Project)” 节点,点击 数据库(Database) 按钮,恢复之前的设置。
然后,在新数据项的 服务端模块(Server module) 中添加代码,实现对数据库中表的数据读写。
下面是 MySQL 数据库(自增主键字段)的代码示例:
import MySQLdb
from jam.db import mysql
def on_open(item, params):
connection = item.task.create_connection_ex(mysql, database='demo', \
user='root', password='111', host='localhost', encoding='UTF8')
try:
sql = item.get_select_query(params, mysql)
rows = item.task.select(sql, connection, mysql)
finally:
connection.close()
return rows, ''
def on_apply(item, delta, params):
connection = item.task.create_connection_ex(mysql, database='demo', \
user='root', password='111', host='localhost', encoding='UTF8')
try:
sql = delta.apply_sql(params, mysql)
result = item.task.execute(sql, None, connection, mysql)
finally:
connection.close()
return result
如果数据库使用生成器(generators)获取主键值(如 Firebird),则必须为新记录指定主键:
import fdb
from jam.db import firebird
def on_open(item, params):
connection = item.task.create_connection_ex(firebird, database='demo.fdb', \
user='SYSDBA', password='masterkey', encoding='UTF8')
try:
sql = item.get_select_query(params, firebird)
rows = item.task.select(sql, connection, firebird)
finally:
connection.close()
return rows, ''
def get_id(table_name, connection):
cursor = connection.cursor()
cursor.execute('SELECT NEXT VALUE FOR "%s" FROM RDB$DATABASE' % (table_name + '_SEQ'))
r = cursor.fetchall()
return r[0][0]
def on_apply(item, delta, params):
connection = item.task.create_connection_ex(firebird, database='demo.fdb', \
user='SYSDBA', password='masterkey', encoding='UTF8')
for d in delta:
if not d.id.value:
d.edit()
d.id.value = get_id(item.table_name, connection)
for detail in d.details:
for r in detail:
if not r.id.value:
r.edit()
r.id.value = get_id(r.table_name, connection)
r.post()
d.post()
try:
sql = delta.apply_sql(params, firebird)
result = item.task.execute(sql, None, connection, firebird)
finally:
connection.close()
return result
你也可以在任务的 on_open 和 on_apply 事件中处理。下面是任务客户端模块的代码示例:
import MySQLdb
from jam.db import mysql
def on_open(item, params):
if item.item_name in ['table1', 'table2']: # or
#if item.table_name in ['table1', 'table2']:
connection = item.task.create_connection_ex(mysql, database='demo', \
user='root', password='111', host='localhost', encoding='UTF8')
try:
sql = item.get_select_query(params, mysql)
rows = item.task.select(sql, connection, mysql)
finally:
connection.close()
return rows, ''
def on_apply(item, delta, params):
if item.item_name in ['table1', 'table2']:
connection = item.task.create_connection_ex(mysql, database='demo', \
user='root', password='111', host='localhost', encoding='UTF8')
try:
sql = delta.apply_sql(params, mysql)
result = item.task.execute(sql, None, connection, mysql)
finally:
connection.close()
return result
MSSQL 示例:
import pymssql
from jam.db import mssql
def on_open(item, params):
connection = item.task.create_connection_ex(mssql, database='jam7', \
user='sa', password='password', server='127.0.0.1', port='1433')
try:
sql = item.get_select_query(params, mssql)
rows = item.task.select(sql, connection, mssql)
finally:
connection.close()
return rows, ''
def on_apply(item, delta, params):
connection = item.task.create_connection_ex(mssql, database='jam7', \
user='sa', password='password', server='127.0.0.1', port='1433')
try:
sql = delta.apply_sql(params, mssql)
result = item.task.execute(sql, None, connection, mssql)
finally:
connection.close()
return result
备注
不要为这些表设置 History 属性为 True,否则会报错。历史表在项目中所有数据库只能有一个。
你可以尝试在其他数据库中创建历史表,并为其编写 on_open 和 on_apply 事件处理程序。
备注
上述方法未在 Jam.py V7 下测试。
更简单的方案是使用数据库同义词(synonyms)。