Cursor.execute does not work in Python - python

I am using MySQLdb package in Python to update my database. I have a simple update command as follows :
update_query = "update user_details set `address`='%s' where `id`='%s'"
cursor.execute(update_query, (address, id1))
print(cursor._last_executed)
Here is the command executed :
update user_details set `address`='35, Chikmagalur' where `id`='242069'
The program runs fine without error. However, the database is not getting updated. The same command works when I run as an SQL query on PHPMyAdmin.
Any idea what could be the issue ?

this is a duplicate of ...
sql transactions needs to be committed, either explicitly or implicitly.
either issue a commit command explicitly
cursor._get_db().commit()
setting the connection to autocommit when opening the connection is also an option.

Related

Python 2.5 informixdb connect parameters

I use Python 2.5 and informixdb. I want to connect with the database, but what are the parameters for the informixdb.connect() method?
I have
Hostname
Port
Databasename
User
Password
But what is the right order? Or how is the dsn String build?
The official documentation does not really help me.
The documentation says i can use
informixdb.connect(dsn)
but they do not explain how a DataSourceString should looks like. What arguments and in which order are needed.
Here is an link to the documentation.
And i know Python 2.5 is very old, but the database does not support Python 3.x i have tried it.
From the documentation at https://sourceforge.net/projects/informixdb/:
To do anything useful with InformixDB one must connect to a database. This is accomplished by calling informixdb.connect:
>>> import informixdb
>>> conn = informixdb.connect('db#daniel', user='me', password='something')
>>> conn
<_informixdb.Connection object at 0xb7d08e90>
informixdb.connect takes three arguments: A dsn which identifies the database and server to connect to, as recognized by ESQL's CONNECT statement (e.g. 'database#server', 'database', '#server') plus an optional user and a corresponding password.
If the dsn doesn't include a servername the value of the environment variable INFORMIXSERVER is used. When connecting without specifying the name of the database no database will be selected. This is useful for setting up a new database from within InformixDB.
Why not use the new "IfxPy" OpenInformix module?
https://github.com/OpenInformix/IfxPy
It has support for both 2.x and 3.x versions of Python.

Running multiple statements with MySQLdb

I'd like to run multiple statements with one command. Is it possible:
This is the SQL command:
UPDATE toggle SET state='0' WHERE feature_name=‘feature_1;
UPDATE toggle SET state=‘1’ WHERE feature_name=‘feature_2’;
UPDATE toggle SET state=‘1’ WHERE feature_name=‘feature_3’;
For one command I run something like that:
import MySQLdb
myDB = MySQLdb.connect(host=host, port=db_port, user=user, passwd=db_password, db=db)
cHandler = myDB.cursor()
cHandler.execute(query)
But this obviously works only for a single statement
Thanks!
Use parameterized query and executemany.

linking mongoDB to python code?

still very new to programming so in my quest to learn git i am working on getting a chat bot for telegram up and running that i found on a github. iv never used mongoDB or any database and i see i need
DB_NAME=os.environ['DB_NAME']
DB_USERNAME=os.environ['DB_USERNAME']
DB_HOST=os.environ['DB_HOST']
DB_PASSWORD=os.environ['DB_PASSWORD']
DB_URL="dbname='"+DB_NAME+"' user='"+DB_USERNAME+"' host='"+DB_HOST+"' password='"+DB_PASSWORD+"'"
conn=psycopg2.connect(DB_URL)
cur=conn.cursor()
cur.execute("SELECT chat_id FROM users")
users=cur.fetchall()
id_list=[chat_id[0] for chat_id in users]
cur.close()
conn.close()
the DB_NAME=os.environ['DB_NAME']
where does DB_NAME come from and why does os.environ throw an error. im using python 3. the github is here https://github.com/trinhvv/trading-analysis-bot
os.environ["DB_NAME"] means that DB_NAME is stored in the system environment variables.
You can connect to the mongodb by:
Option 1:
Declare variables in system environment.
Option 2:
declaring enviornment variables in the code itself.
Environment variables must be strings, so use,
os.environ["DB_NAME"] = "your_db_name"
Option 3:
Don't use variables from environment, write it directly,
DB_NAME = "your_db_name"

Oracle Python callproc() Script stops

I have a hughe problem.
I created a procedure and stored it in my OracleDB.
Now I want to execute the procedure, but its not working. There is no Error-Message Occuring, it just stops and dont continue after the first callproc call. Anybody any ideas?
!Autoincrement is enabled for the whole script!
import cx_Oracle dsn= cx_Oracle.makedsn("**********,1521,"orcl")
db= cx_Oracle.connect('******','*******',dsn)
db.autocommit = True cur = db.cursor()
cur.callproc("UPDATE_MISSING_VALUES", ['GENDER','***_PRE',1])
The Procedure is found and if I change the parameters, an SQL Error occurs. But if I let it like that, nothing happens.
If I run that query in SQL-Developer, it works just fine
It works now, I have actually now idea why, but the most important thing is that it works.
Is it possible, that the reason is that I made a
COMMIT;
Statement in my DB?? After that it worked suddenly.

How to execute an SQL query with Python script in MySQL Workbench

I want to execute SQL queries from the Python script environment in MySQL Workbench. I looked at the MySQL Workbench documentation for the grt module and found the executeScript method but I can't seem to use it to make queries.
Executing this Python code:
import grt
querystring = "select * from Purchases WHERE PurchaseAmount > 600 and PurchaseAmount < 2500"
executeScript(querystring)
produces the following error message:
Uncaught exception while executing [filepath]runquery.py:
File "[filepath]runquery.py", line 10, in <module>
executeScript(querystring)
NameError: name 'executeScript' is not defined
I don't understand what virtual grt::ListRef executeScript ( const std::string & sql ) means so I can't format my query properly, however, the error message seems to indicate that the executeScript method doesn't exist anyway. Most documentation I look at has examples of correctly-formatted function calls but I can't seem to find any for executeScript.
All I want to do is literally run my string as an SQL query within the MySQL Workbench Python scripting environment.
Thank you!
I am new to Python and SQL so please be patient. :)
To run executeScript function you need to interact with an sqleditor object.
For testing, do the next on MS Windows with the example databases:
Start MySQLWorkbench
connect to local database
select sakila from SCHEMAS
start scripting shell with Tools->scripting shell or (Ctrl+F3)
Add new python script (test.py)
Save script with the content below
run script in scripting shell
Script content:
import grt
result = grt.root.wb.sqlEditors[0].executeScript("select * from actor limit 10;")
for col in result[0].columns:
print col.name
To find out how to reference objects in the script, it is very easy to use the Globals Tree panel's class browser and using right mouse click on the object and choose "Copy Path for Python"
You can run something like the following command if you need to run your script from command line in Windows:
"C:\Program Files\MySQL\MySQL Workbench 6.1 CE\MySQLWorkbench.exe" -query "Local instance MySQL56" -run-python "execfile('c:\Users\Root\AppData\Roaming\MySQL\Workbench\scripts\script.py')" -log-to-stderr -v
The (first) problem seems to be that you use a function called executeScript(), which you haven't defined or taken from anywhere. If it is in the grt module (which I am not familiar with) you have to do it as follows:
from grt import executeScript
querystring = "select * from Purchases WHERE PurchaseAmount > 600 and PurchaseAmount < 2500"
executeScript(querystring)

Categories