简体   繁体   中英

Insert data in oracle database in python

I can't insert data into database using a dynamic query in python script

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.

I found the solution to the problem connection.commit()

You can use format method in Python like below:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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