我正在使用SQLAlchemy管理2个数据库。一个是本地SQLite数据库,另一个是Google的云平台上的远程MySQL数据库,它们都共享相同的架构。我正在尝试使用将SQLite数据库中包含的行追加到MySQL数据库。我目前有一个定义我的班级的文件,如下所示:
SensorCollection.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)
然后,我还有另一个文件声明了我的Base,Session和engine:
base.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错误。它也不会给我任何错误。它可以编译并运行,但不附加数据甚至不创建表。这是我第一次尝试破解任何一种数据库,因此我确信它充满了错误,而且一点也不优雅。任何帮助,将不胜感激。
编辑
djkern帮助我创建了表,并且我更新了代码。不过,我仍然没有要附加的数据。
最佳答案
在您的代码中,您具有:
# create the Google db and start the session
BaseLocal.metadata.create_all(engineLocal)
sessionGoogle = SessionGoogle()
你是说要成为这个吗?
# create the Google db and start the session
BaseGoogle.metadata.create_all(engineGoogle)
sessionGoogle = SessionGoogle()
关于python - 如何使用SQLAlchemy将行从本地SQLite数据库迁移到远程MySQL数据库?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49912226/