我正在使用sqlalchemy管理2个数据库。一个是本地sqlite数据库,另一个是google云平台上的远程mysql数据库,它们共享相同的模式。我试图使用append将sqlite数据库中包含的行添加到mysql数据库中。我目前有一个文件定义了我的类,如下所示:
传感器采集.py
class Readings(Base):
__tablename__ = 'readings'
time = Column(String(250), primary_key=True)
box_name = Column(String(250))
FS = Column(Numeric)
IS = Column(Numeric)
VS = Column(Numeric)
CO = Column(Numeric)
TVOC = Column(Numeric)
cTemp = Column(Numeric)
fTemp = Column(Numeric)
humidity = Column(Numeric)
pressure = Column(Numeric)
然后我有另一个文件声明我的基、会话和引擎:
基本.py
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///%s' % 'db/db.db')
Session = sessionmaker(bind=engine)
Base = declarative_base()
最后是我的实际代码:
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.schema import Table
from SensorCollection import Readings
from sqlalchemy.sql.expression import insert
# uploads MySQL database to Google cloud server
# uses ORM to ensure the databases can communicate with each other
user =***
password =***
host =***
port =***
dbName =***
instanceConnectionName =***
# create the engine for the local db
engineLocal = create_engine('sqlite:///%s' % 'db/db.db')
BaseLocal = Readings(None, None, None, None, None, None, None, None, None, None, None)
# create the engine for the Google db
engineGoogle = create_engine('mysql+mysqldb://%s@%s:%s/%s?unix_socket=/cloudsql/%s' % (user, host, port, dbName, instanceConnectionName))
SessionGoogle = sessionmaker(bind=engineGoogle)
BaseGoogle = Readings(None, None, None, None, None, None, None, None, None, None, None)
# create the local db
BaseLocal.metadata.create_all(engineLocal)
# create the Google db and start the session
BaseGoogle.metadata.create_all(engineGoogle)
sessionGoogle = SessionGoogle()
# create table objects for each db
t1 = Table('readings', BaseLocal.metadata, autoload=True, autoload_with=engineLocal)
t2 = Table('readings', BaseGoogle.metadata, autoload=True, autoload_with=engineGoogle)
# the first subquery, select all ids from SOME_TABLE where some_field is not NULL
s1 = t1.select()
# the second subquery, select all ids from SOME_TABLE where some_field is NULL
s2 = t2.select()
# union s1 and s2 subqueries together and alias the result as "alias_name"
q = s1.union(s2)
insert(t2, s1)
sessionGoogle.query(q)
# sessionGoogle.add_all(s1)
# commit changes and close the session
sessionGoogle.commit()
sessionGoogle.close()
目前,sqlite数据库已经存在,远程服务器已经设置好,但是当它连接到google时,代码甚至没有创建表。它似乎连接正确,因为如果我改变任何服务器的细节,我会得到一个mysql错误。它也不会给我任何错误。它编译并运行,但不附加数据,甚至不创建表。这是我第一次尝试任何类型的数据库,所以我敢肯定它充满了错误,一点也不优雅。任何帮助都将不胜感激。
edit djkern帮助我创建了表,并更新了代码。不过,我还没有得到要追加的数据。
2条答案
按热度按时间a6b3iqyw1#
还要加两个班,
以及
它们应该位于与相同的文件中,并且与相同
还必须创建新的base、engine和session变量。然后,代码应该如下所示:
rjzwgtxy2#
在代码中,您有:
你是故意的吗?