通过python将变量的值插入数据库
我是Python新手,因此在这方面需要一些帮助: 在这里,我尝试在数据库中插入值,当我尝试给出硬编码的值时,插入发生了 注意:common_test2只有2个字 但当我像下面这样写的时候:通过python将变量的值插入数据库,python,database,insert,Python,Database,Insert,我是Python新手,因此在这方面需要一些帮助: 在这里,我尝试在数据库中插入值,当我尝试给出硬编码的值时,插入发生了 注意:common_test2只有2个字 但当我像下面这样写的时候: import cx_Oracle con = cx_Oracle.connect('sys/sys@127.0.0.1:1599/xe') print(con.version) //just to check the connection print("this connection is establish
import cx_Oracle
con = cx_Oracle.connect('sys/sys@127.0.0.1:1599/xe')
print(con.version) //just to check the connection
print("this connection is established") //connection is tested
cur=con.cursor()
f3= open("common_test2", 'r+')
string= f3.read()
common_words=string.split()
x=common_words[0]
y=common_words[1]
cur.execute("INSERT INTO test(name,id) VALUES (%s,%d)", ('hello',30))
con.commit()
错误是
错误为cx\U Oracle。数据库错误:ORA-01036:非法变量名称/编号
还尝试了cur.execute(“插入测试(名称,id)值(x,y)”)
但是没有运气
错误为cx\U Oracle。数据库错误:ORA-00984:此处不允许列
有什么帮助吗
#I am using this for updating the table
y=100%
x=text
cur.execute("update temp set perc=(:1)", (y))
#Please note: I only want 100 to be updated in the table not the % (only 100 as numeric)
cur.execute("update temp set remarks=(:1)",(x))
错误来自这里:
cur.execute("INSERT INTO test(name,id) VALUES (%s,%d)", ('hello',30))
尝试使用:n
指针:
cur.execute("INSERT INTO test(name, id) VALUES (:1, :2)", ('hello', 30))
更新 对于第二种情况-如果
y
是一个类似y=“100%”
的字符串,则可以通过以下方式进行更新:
cur.execute("update temp set perc = :1", (y[:-1],))
这将插入100
作为int
请注意,1项大小的元组是
(x,)
,不是(x)
我正在使用的代码
注意:我必须检索10并做进一步的计算
表温度:
perc备注
10增加了按照您的建议尝试,但出现错误cx_Oracle.DatabaseError:ORA-01036:非法变量名称/编号您能帮我使用动态更新命令y=100%cur.execute(“更新临时设置perc=(:1)”,(y))注意:我只希望输入100,而不是“%”谢谢。请将此代码添加到您的问题中,我将更新我的答案。尝试了相同的方法,得到了以下错误:cx\u Oracle.NotSupportedError:Variable\u TypeByValue():未处理的数据类型元组请不要使用“update”块。将来来到这里的人不关心帖子的编辑历史:
import cx_Oracle
con = cx_Oracle.connect('system/system@127.0.0.1:1521/xe')
# print(con.version)
#
# print("this connection is established")
cur = con.cursor()
f3 = open("common.txt", 'r+') #this text file have only 2 words lets say (10% increased)
string = f3.read() #will read common.txt
common_words = string.split()
x = common_words[0] #here x have 10%
y = common_words[1] #here y have increased
# Now I want 10 should be updated in the temp table **Not 10%**
cur.execute("update temp set perc=(:1)", (y[:-1],))
cur.execute("update temp set remarks=(:1)", (y))
con.commit