简体   繁体   English

在 python 的 oracle 数据库中插入数据

[英]Insert data in oracle database in python

I can't insert data into database using a dynamic query in python script我无法使用 python 脚本中的动态查询将数据插入数据库

def execute_query(self, qo):
    query_string = "INSERT INTO " +dep_table+ " (client, sis, entity_name_1, entity_name_2, flag_dep,process, flag_dep_det) VALUES (%s, %s, %s, %s, %s, %s, %s)" % ("'CO'","'"+qo.db_src+"'","'"+qo.table_src+"'","'"+qo.table_des+"'","'"+qo.check_func+"'","'"+qo.table_des+"'","'NULL'")+";"
    cursor.execute(query_string)

I got this error:我收到了这个错误:

ERROR: Failed to set dependencies informations : ORA-00933: SQL command not properly ended

The connection to the database is okay, but I can't insert.与数据库的连接没问题,但我无法插入。

Drop the semi-colon at the end of the string you are creating / executing.在您正在创建/执行的字符串末尾删除分号。

It shouldn't be part of the SQL statement, rather used in some client tools to indicate the end of a statement so that the client can send it to the database to be executed.它不应该是 SQL 语句的一部分,而是在某些客户端工具中用于指示语句的结束,以便客户端可以将其发送到数据库以执行。

I found the solution to the problem connection.commit()我找到了问题connection.commit()的解决方案

You can use format method in Python like below:您可以在 Python 中使用format方法,如下所示:

def execute_query(self, qo):
    query_string = "INSERT INTO {0} (client, sis, entity_name_1, entity_name_2, flag_dep,process, flag_dep_det) VALUES ('{1}', '{2}', '{3}', '{4}', '{5}', '{6}', {7})".format(dep_table, 'CO', qo.db_src, qo.table_src, qo.table_des, qo.check_func, qo.table_des, 'NULL')
    cursor.execute(query_string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM