将Python变量插入Oracle数据库
我可以成功地将整个数据帧插入到Oracle表中,但我不知道如何将单个变量插入到Oracle表中。我使用绑定变量和常规Python变量都失败了 在本例中,我的变量将Python变量插入Oracle数据库,python,sql,oracle,Python,Sql,Oracle,我可以成功地将整个数据帧插入到Oracle表中,但我不知道如何将单个变量插入到Oracle表中。我使用绑定变量和常规Python变量都失败了 在本例中,我的变量year打印出2014,因为我的代码解析我运行它的文档的年份;它最终必须在100个文档上运行 因此,我的SQL语句如下所示: cursor = con.cursor() exported_data = [tuple(x) for x in df_Quota.values] new_variable = year sql_query = (
year
打印出2014
,因为我的代码解析我运行它的文档的年份;它最终必须在100个文档上运行
因此,我的SQL语句如下所示:
cursor = con.cursor()
exported_data = [tuple(x) for x in df_Quota.values]
new_variable = year
sql_query = ("INSERT INTO ROUGHTABLE(species, date_posted, stock_id, pounds, money, sector_name, ask)" "VALUES (:1, :2, :3, :4, :5, 'NEFS 2', '1')")
year_command = ("INSERT INTO ROUGHTABLE(trade_year)" "VALUES (:1)")
cursor.executemany(sql_query, exported_data)
cursor.executemany(year_command, new_variable)
con.commit()
cursor.close()
con.close()
这将失败,出现错误cx\u Oracle.DatabaseError:ORA-01036:非法变量名/编号
。。。如果我只是尝试以下操作,它也会失败:
year_command = ("INSERT INTO ROUGHTABLE(trade_year)" "VALUES (year)")
cursor.execute(year_command)
错误为cx\U Oracle.DatabaseError:ORA-00984:此处不允许列
因此,对于如何将单个Python变量放入Oracle表中,我感到非常困惑。理想情况下,我希望将变量year
的值导出到Oracle表中的trade\u year
列。如果您能帮助解决此问题,我们将不胜感激
df_Quota:
AvailableQuota DatePosted ID LiveWeightPounds price
0 GOM COD 5/20 1724 2328 $9,000
1 GOM HADD 5/20 1724 445 $9,000
2 GOM BB 5/20 1724 3007 $9,000
3 GREYSOLE 5/20 1724 850 $9,000
4 DABS 5/20 1724 3101 $9,000
5 GOM YT 5/20 1724 1995 $9,000
6 GBE COD 5/20 1578 538 $1.00
7 GB BB 5/20 1578 1755 $0.20
8 GB YT 5/20 1578 243 $1.00
9 SNE BB 5/20 1578 490 $0.45
10 SNE YT 5/20 1578 153 $0.50
11 GOM BB 5/20 1578 3965 $0.15
12 Whake 5/20 1578 2727 $0.20
13 POLL 5/20 1578 9227 $0.01
14 RED 5/20 1578 15060 $0.01
15 GBE COD 5/20 310 825 15,000
16 GBW COD 5/20 310 9033 15,000
17 DABS 5/20 310 12419 15,000
18 WHAKE 5/20 310 3120 15,000
19 POLL 5/20 310 65234 15,000
20 RED 5/20 310 76610 15,000
21 SNE BB 5/20 310 2121 15,000
22 GOM BB 5/20 310 7285 15,000
下面是一个简单的例子:
import cx_Oracle
import csv,sys
dsnStr = cx_Oracle.makedsn("xxxxxxxxxxxxx", "1521", "xxxxxxxx")
con = cx_Oracle.connect(user="scott", password="tiger", dsn=dsnStr)
print "Database version " + con.version
myCur = con.cursor()
myCur.prepare( 'insert into roughtable( trade_year ) values ( :theYear )' )
myYear='2016'
myCur.execute(None, {'theYear':myYear})
con.commit()
con.close()
下面是一个简单的例子:
import cx_Oracle
import csv,sys
dsnStr = cx_Oracle.makedsn("xxxxxxxxxxxxx", "1521", "xxxxxxxx")
con = cx_Oracle.connect(user="scott", password="tiger", dsn=dsnStr)
print "Database version " + con.version
myCur = con.cursor()
myCur.prepare( 'insert into roughtable( trade_year ) values ( :theYear )' )
myYear='2016'
myCur.execute(None, {'theYear':myYear})
con.commit()
con.close()
好奇的是,你为什么不使用pandas'来插入数据框?我其实不知道pandas有这个功能…我有点不喜欢pandas,直到最近才发现它也可以读取htmlCurious,为什么不在数据框插入中使用pandas?我不知道pandas有这个功能…我有点喜欢pandas,直到最近才发现它也可以读取。但是,我想将它设置为一个变量,而不是将
myYear
设置为'2016',因为在我运行代码的每个文档中,它将解析出文档中的任何年份,因此该值将更改。@浏览器是的,我理解。myYear是一个变量。因此,您可以将其设置为程序中所需的任何内容。我向您展示的是如何将一个程序变量绑定到sql语句。现在,如果您想在数据库中写入一堆myYears,您可以填充一个数组(我认为是python术语的列表)并绑定该数组,然后执行“数组插入”(Oracle术语)Ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh。给我一点时间,我现在就试试,thanks@theprowler我将在大约一小时后添加一个数组插入示例(我需要签入航班:),但我不想将myYear
设置为'2016',而是想将其设置为一个变量,因为在我运行代码的每个文档中,它将解析出文档中的任何年份,所以那个值会改变。@是的,我理解。myYear是一个变量。因此,您可以将其设置为程序中所需的任何内容。我向您展示的是如何将一个程序变量绑定到sql语句。现在,如果您想在数据库中写入一堆myYears,您可以填充一个数组(我认为是python术语的列表)并绑定该数组,然后执行“数组插入”(Oracle术语)Ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh。给我一点时间,我现在就试试,thanks@theprowler我将在大约一小时内添加一个数组插入示例(我需要签入航班:)