I've written a simple python program, which get Data form database successfully. but unable to update table in DB.
When executing update statement it's get stuck and nothing happen, no any exception.
My code is as follows. Any idea whyis this ?
from java.sql import DriverManager
def updateDB():
url = "jdbc:oracle:thin:#192.1.1.1:1521:auid"
uname = "dbtstj1"
pword = "dbtstj321"
conn = None
stmt = None
try:
conn = DriverManager.getConnection(url,uname,pword)
stmt = conn.createStatement()
rs = stmt.executeQuery("select PKG_NAME from PkgData")
while rs.next():
print rs.getString(1)
pkgName = "'Test Pkg Name'"
pkgID = "'T1234'"
updateQuary = "UPDATE PkgData SET PKG_NAME =%s WHERE PKG_ID =%s" %(pkgName, pkgID)
stmt.execute(updateQuary)
except Exception , e:
print 'Error:', e[0]
finally:
if stmt is not None:
stmt.close()
if conn is not None:
conn.close()
updateDB()
you need to commit your changes to the database:
stmt.execute(updateQuary)
conn.commit()
These type of issues can be happen when query request datatype and required datatype is difference.
It seems to be there was a mismatch with database's datatype and your query. Can you recheck with database's datatype with your query.
For Ex: PKG_ID =%s can be another data type in database as digit or etc...
Related
Im trying to pass a string in my update query which is read from a text file(this have only one string)
Also im trying to update a oracle database table
My code looks like this
try:
with open('update.txt') as file1:
upd_value = up.read() # This gives me upd_value = 'HELLO_THERE'
conn = cx_Oracle.connect('usr', 'pwd', dsn_tns)
cursor = conn.cursor()
upd_query = """ update some_table set something = %s where some_column = 'some_value',
(upd_value,) """
cursor.execute(upd_query)
except cx_Oracle.DatabaseError as exc:
err, = exc.args
print("Oracle-Error-Code:", err.code)
print("Oracle-Error-Message:", err.message)
finally:
cursor.close()
conn.close()
Im getting the following error
Oracle-Error-Code: 911
Oracle-Error-Message: ORA-00911: invalid character
It would be great if somebody help me out with this, Thanks in advance!
I have some python code that gets data from one database (SQL server) and inserts it into another database (MySQL). I am trying to add a WHERE NOT EXIST to the INSERT query so only new rows are inserted, but need to use one of the values in the tuple SageResults a second time for the primary key.
Code:
import mysql.connector
import pyodbc
def insert_VPS(SageResult):
query = """
INSERT INTO SOPOrderReturn(SOPOrderReturnID, DocumentTypeID, DocumentNo, DocumentDate, CustomerID, CustomerTypeID, CurrencyID, SubtotalGoodsValue, TotalNetValue, TotalTaxValue, TotalGrossValue, SourceTypeID, SourceDocumentNo)
VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
WHERE NOT EXISTS (SELECT * FROM SOPOrderReturn WHERE SOPOrderReturnID = %1$s)"""
try:
mydbVPS = mysql.connector.connect(
host="address",
user="user",
passwd="password",
database="database"
)
VPScursor = mydbVPS.cursor()
#print(SageResult)
VPScursor.executemany(query, SageResult)
mydbVPS.commit()
except Exception as e:
print('InsertError:', e)
finally:
VPScursor.close()
mydbVPS.close()
def main():
selectQuery = """
SELECT TOP 51 [SOPOrderReturnID]
,[DocumentTypeID]
,[DocumentNo]
,[DocumentDate]
,[CustomerID]
,[CustomerTypeID]
,[CurrencyID]
,[SubtotalGoodsValue]
,[TotalNetValue]
,[TotalTaxValue]
,[TotalGrossValue]
,[SourceTypeID]
,[SourceDocumentNo]
FROM [Live].[dbo].[SOPOrderReturn]
"""
try:
mydbSage = pyodbc.connect('Driver={SQL Server};'
'Server=CRMTEST;'
'Database=Live;'
'UID=sa;'
'PWD=password;')
Sagecursor = mydbSage.cursor()
Sagecursor.execute(selectQuery)
#SageResult = tuple(Sagecursor.fetchall())
SageResult = []
while True:
row = Sagecursor.fetchone()
if row:
SageResult.append(tuple(row))
else:
break
#SageResult = Sagecursor.fetchall()
mydbSage.commit()
except Exception as e:
print('MainError:', e)
finally:
Sagecursor.close()
mydbSage.close()
insert_VPS(SageResult)
if __name__ == '__main__':
main()
Output:
D:\xampp\htdocs\stripe\group\beta>sql-sync.py
InsertError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use ne
ar 'WHERE NOT EXISTS (SELECT * FROM SOPOrderReturn WHERE SOPOrderReturnID = %1$s),(1' at line 3
The part in question is the query string variable. Everything else in here works fine. I basically need to use the SOPOrderReturnID value from the tuple a second time where I currently have %1$s
What is the issue with the query syntax? Is my use of %1$s correct?
I try to Update my DB via Input.
import sqlite3
conn = sqlite3.connect('datenbank.db')
print ("Opened database successfully")
kunde1 = input("Der Kundename: ")
anzahl1 = input("Anzahl Bewertung: ")
conn.execute('''UPDATE kundenname SET anzahl = ? WHERE kundename = ?''',
(anzahl1, kunde1))
conn.commit
it dosent Show any Errors.. but it get not updated in DB?
Thanks a lot !
conn.commit does not call commit, rather, it is merely accessing its instance. Therefore, a simple commit call and a file close should fix the problem:
import sqlite3
conn = sqlite3.connect('datenbank.db')
print ("Opened database successfully")
kunde1 = input("Der Kundename: ")
anzahl1 = input("Anzahl Bewertung: ")
conn.execute('''UPDATE kundenname SET anzahl = ? WHERE kundename = ?''', (anzahl1, kunde1))
conn.commit()
conn.close()
since you are inputting data in the middle of the execution sequence, it may be better to use a contextmanager:
import contextlib
#contextlib.contextmanager
def update_database(kunde1, anzahl1):
conn = sqlite3.connect('datenbank.db')
print ("Opened database successfully")
yield conn #can be used later to update database in or outside the scope of "with"
conn.execute('''UPDATE kundenname SET anzahl = ? WHERE kundename = ?''', (anzahl1, kunde1))
conn.commit()
conn.close()
with update_database(input("Der Kundename: "), input("Anzahl Bewertung: ")) as b:
pass #do something after
I know that this question has been asked in the past, but thorough searching hasn't seemed to fix my issue. I'm probably just missing something simple, as I'm new to the Python-mysql connector supplied by mysql.
I have a Python script which accesses a mysql database, but I'm having issues with removing quotes from my query. Here is my code:
import mysql.connector
try:
db = mysql.connector.connect(user='root', password='somePassword', host='127.0.0.1', database='dbName')
cursor = db.cursor()
query = "select * from tags where %s = %s"
a = 'tag_id'
b = '0'
cursor.execute(query, (a, b))
print cursor
data = cursor.fetchall()
print data
except mysql.connector.Error as err:
print "Exception tripped..."
print "--------------------------------------"
print err
cursor.close()
db.close()
My database is set up properly (as I'll prove shortly).
My output for this program is:
MySQLCursor: select * from tags where 'tag_id' = '0'
[]
Yet when I change my query to not use variables, for example:
cursor.execute("select * from tags where tag_id = 0")
Then my output becomes:
MySQLCursor: select * from tags where tag_id = 0
[(0, u'192.168.1.110')]
To me, this means that the only difference between my Cursor queries are the quotes.
How do I remove them from the query?
Thanks in advance.
I personally believe this code is correct and safe, but you should be extremely skeptical of using code like this without carefully reviewing it yourself or (better yet) with the help of a security expert. I am not qualified to be such an expert.
Two important things I changed:
I changed b = '0' to b = 0 so it ends up as a number rather than a quoted string. (This part was an easy fix.)
I skipped the built-in parameterization for the column name and replaced it with my own slight modification to the escaping/quoting built in to mysql-connector. This is the scary part that should give you pause.
Full code below, but again, be careful with this if the column name is user input!
import mysql.connector
def escape_column_name(name):
# This is meant to mostly do the same thing as the _process_params method
# of mysql.connector.MySQLCursor, but instead of the final quoting step,
# we escape any previously existing backticks and quote with backticks.
converter = mysql.connector.conversion.MySQLConverter()
return "`" + converter.escape(converter.to_mysql(name)).replace('`', '``') + "`"
try:
db = mysql.connector.connect(user='root', password='somePassword', host='127.0.0.1', database='dbName')
cursor = db.cursor()
a = 'tag_id'
b = 0
cursor.execute(
'select * from tags where {} = %s'.format(escape_column_name(a)),
(b,)
)
print cursor
data = cursor.fetchall()
print data
except mysql.connector.Error as err:
print "Exception tripped..."
print "--------------------------------------"
print err
cursor.close()
db.close()
I encountered a similar problem using pymysql and have shown my working code here, hope this will help.
What I did is overwrite the escape method in class 'pymysql.connections.Connection', which obviously adds "'" arround your string.
better have shown my code:
from pymysql.connections import Connection, converters
class MyConnect(Connection):
def escape(self, obj, mapping=None):
"""Escape whatever value you pass to it.
Non-standard, for internal use; do not use this in your applications.
"""
if isinstance(obj, str):
return self.escape_string(obj) # by default, it is :return "'" + self.escape_string(obj) + "'"
if isinstance(obj, (bytes, bytearray)):
ret = self._quote_bytes(obj)
if self._binary_prefix:
ret = "_binary" + ret
return ret
return converters.escape_item(obj, self.charset, mapping=mapping)
config = {'host':'', 'user':'', ...}
conn = MyConnect(**config)
cur = conn.cursor()
I need to process mysql data one row at a time and i have selected all rows put them in a tuple but i get the error above.
what does this mean and how do I go about it?
Provide some code.
You probably call some function that should update database, but the function does not return any data (like cursor.execute()). And code:
data = cursor.execute()
Makes data a None object (of NoneType). But without code it's hard to point you to the exact cause of your error.
It means that the object you are trying to iterate is actually None; maybe the query produced no results?
Could you please post a code sample?
The function you used to select all rows returned None. This "probably" (because you did not provide code, I am only assuming) means that the SQL query did not return any values.
Try using the cursor.rowcount variable after you call cursor.execute(). (this code will not work because I don't know what module you are using).
db = mysqlmodule.connect("a connection string")
curs = dbo.cursor()
curs.execute("select top 10 * from tablename where fieldA > 100")
for i in range(curs.rowcount):
row = curs.fetchone()
print row
Alternatively, you can do this (if you know you want ever result returned):
db = mysqlmodule.connect("a connection string")
curs = dbo.cursor()
curs.execute("select top 10 * from tablename where fieldA > 100")
results = curs.fetchall()
if results:
for r in results:
print r
This error means that you are attempting to loop over a None object. This is like trying to loop over a Null array in C/C++. As Abgan, orsogufo, Dan mentioned, this is probably because the query did not return anything. I suggest that you check your query/databse connection.
A simple code fragment to reproduce this error is:
x = None
for each i in x:
#Do Something
pass
This may occur when I try to let 'usrsor.fetchone' execute twice. Like this:
import sqlite3
db_filename = 'test.db'
with sqlite3.connect(db_filename) as conn:
cursor = conn.cursor()
cursor.execute("""
insert into test_table (id, username, password)
values ('user_id', 'myname', 'passwd')
""")
cursor.execute("""
select username, password from test_table where id = 'user_id'
""")
if cursor.fetchone() is not None:
username, password = cursor.fetchone()
print username, password
I don't know much about the reason. But I modified it with try and except, like this:
import sqlite3
db_filename = 'test.db'
with sqlite3.connect(db_filename) as conn:
cursor = conn.cursor()
cursor.execute("""
insert into test_table (id, username, password)
values ('user_id', 'myname', 'passwd')
""")
cursor.execute("""
select username, password from test_table where id = 'user_id'
""")
try:
username, password = cursor.fetchone()
print username, password
except:
pass
I guess the cursor.fetchone() can't execute twice, because the cursor will be None when execute it first time.
I know it's an old question but I thought I'd add one more possibility. I was getting this error when calling a stored procedure, and adding SET NOCOUNT ON at the top of the stored procedure solved it. The issue is that earlier selects that are not the final select for the procedure make it look like you've got empty row sets.
Try to append you query result to a list, and than you can access it. Something like this:
try:
cursor = con.cursor()
getDataQuery = 'SELECT * FROM everything'
cursor.execute(getDataQuery)
result = cursor.fetchall()
except Exception as e:
print "There was an error while getting the values: %s" % e
raise
resultList = []
for r in result:
resultList.append(r)
Now you have a list that is iterable.