【问题标题】:Error on insertion of data after successful opening of connection成功打开连接后插入数据出错
【发布时间】:2015-04-20 07:45:46
【问题描述】:

我正在将数据从 mysql 表插入到 postgres 表中,我的代码是:

from sqlalchemy import create_engine, MetaData, Table

from sqlalchemy.orm import mapper, sessionmaker

import psycopg2

class TestTable(object):

    pass

class StoreTV(object):

    pass

if __name__ == "__main__":

    engine = create_engine('mysql://root@localhost:3306/irt', echo=False)

    Session = sessionmaker(bind=engine)
    session = Session()

    metadata = MetaData(engine)
    test_table = Table('test_1', metadata, autoload=True)

    store_tv_table = Table('roku_store', metadata, autoload=True)

    mapper(TestTable, test_table)
    mapper(StoreTV, store_tv_table)

    res = session.query(TestTable).all()
    print res[1].test_1col

    tv_list = session.query(StoreTV).all()

    for tv in tv_list:
        tv_data = dict()
        tv_data = {
            'title': tv.name,
            'email': tv.business_email
        }
        print tv_data


        conn = psycopg2.connect(database="db", user="user", password="pass", host="localhost", port="5432")
        print "Opened database successfully"
        cur = conn.cursor()
        values = cur.execute("Select * FROM iris_store")
        print values
        cur.execute("INSERT INTO iris_store(title, business_email) VALUES ('title':tv_data[title], 'business_email':tv_data[business_email])")
        print "Record created successfully"

        conn.commit()
        conn.close()

而且我无法从 postgres 数据中获取数据并插入到 postgres 表中 虽然我成功从 Mysql 表中获取数据

错误是:

某事

{'email': 'name@example.com', 'title': "Some Name"}

成功打开数据库

Traceback(最近一次通话最后一次):

文件“/home/Desktop/porting.py”,第 49 行,在 cur.execute("INSERT INTO iris_store(title, business_email) VALUES ('title':tv_data[title], 'business_email':tv_data[business_email])")

psycopg2.ProgrammingError:“:”处或附近的语法错误 第 1 行:... iris_store(title, business_email) VALUES ('title':tv_data[t... ^

【问题讨论】:

    标签: database


    【解决方案1】:

    乌斯曼 您必须检查电子邮件的数据类型才能插入数据。 因为要将数据从 mysql 插入到 postgres,您必须将两个字段都插入相同类型的字段。 click here and page no 28 will describe you aboout the data types of mysql and postgres

    【讨论】:

      【解决方案2】:

      您的主要问题是您的插入查询中有一个 sql 语法错误。它应该看起来像这样:

      cur.execute("INSERT INTO iris_store(title, business_email) VALUES (%(title)s, %(email)s)", tv_data)
      

      参考见:Passing parameters to SQL queries

      另外,您可能不想为 tv_list 中的每个单个值创建与您的 postgres 数据库的新连接,您应该将连接和关闭调用移到 for 循环之外,并分别打印整个表时间似乎也不是很有用

      【讨论】:

      • 这次我收到此错误:文件“/home/usmanmaqbool/Desktop/porting.py”,第 49 行,在 cur.execute("INSERT INTO iris_store(title, business_email) VALUES (%(title)s, %(email)s)", tv_data) psycopg2.DataError: array value must start with "{" or dimension information LINE 1: ...re(title, business_email) VALUES ('Naeem' 's Biz'、'naeem.ily...
      • 我从表中删除了 ('s) 然后在执行时出现此错误:cur.execute("INSERT INTO iris_store(title, business_email) VALUES (%(title)s, %(email) s)", tv_data) psycopg2.DataError: array value must start with "{" or dimension information LINE 1: ...store(title, business_email) VALUES ('Naeem Biz', 'naeem.ily...
      • 最后我得到了我的错误:错误是因为电子邮件列的数据类型。谢谢@mata
      猜你喜欢
      • 2014-12-04
      • 1970-01-01
      • 2014-10-05
      • 2011-05-19
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多