def connect_to_db():
mydb = mysql.connector.connect(
host=CONFIG['host'],
port = CONFIG['port'],
user=CONFIG['username'],
passwd=CONFIG['password'],
database=CONFIG['database']
)
class MyTable:
def __init__(self, conn, table_name):
self.table_name = table_name
self.conn = conn
self.cursor = conn.cursor()
self.commit = conn.commit()
def update(self, whereD, valueD):
if isinstance(whereD, tuple):
where = "%s = '%s'" % whereD
else:
return "error"
kv = ' , '.join(["%s = '%s'" % (k, v) for k, v in valueD.items()])
sql = "UPDATE %s SET %s WHERE %s " % (self.table_name, kv, where)
print(sql)
self.conn
self.cursor
(self.cursor).execute(sql)
self.commit
and I tried to use this class like this:
mydb = connect_to_db()
tab = MyTable(connect_to_db(), "test")
tab.update(('name', 'aaa'), {'age': 800})
The original data is name:aaa, age:20
I connected to mysql by using aws. And I want to use UPDATE query by class MyTable.
But it wasn't worked. Is there something wrong about my code?
cur.execute("Update table set column= ? where cond= ?",[a,b])
You can try this code in update query
Thanks
Related
I have made a function to tidy up the SQL calls I will be doing in my program.
Normally this will work
mycursor = mydbr.cursor()
mycursor.execute("SELECT song FROM charts WHERE artist=%s",(artist,))
x = mycursor.fetchone()
My function looks like this
import mysql.connector
def SqlQuery(connection, query, forvar1 = None, forvar2 = None):
sqlobj = connection.cursor()
if forvar1 is None and forvar2 is None:
sqlobj.execute(query)
if forvar1 is not None and forvar2 is None:
sqlobj.execute(query, forvar1)
if forvar1 is not None and forvar2 is not None:
queryvars = (forvar1,forvar2)
sqlobj.execute(query % queryvars)
result = sqlobj.fetchone()
connection.commit
return result
List of queries
sql = ["SELECT song FROM charts WHERE artist=%s"]
My function call looks like this
record = SqlQuery(mydbr, sql[0], artist)
But whenever it runs it misses adding the variable at all, I have watched what is being sent through wireshark and that looks like
SELECT song FROM charts WHERE artist=%s
Any help would be great
Damn I tried so many variations but this site had the right one
https://pynative.com/python-mysql-select-query-to-fetch-data/
sqlobj.execute(query, forvar1)
Should have been
sqlobj.execute(query, (forvar1,))
Please, proper syntax and i described as per below:
import mysql.connector
def SqlQuery(connection, query, forvar1 = None, forvar2 = None):
sqlobj = connection.cursor()
if forvar1 is None and forvar2 is None:
sqlobj.execute(query)
if forvar1 is not None and forvar2 is None:
sqlobj.execute(query, (forvar1, ) )
if forvar1 is not None and forvar2 is not None:
queryvars = (forvar1,forvar2, )
sqlobj.execute(query, queryvars)
result = sqlobj.fetchone()
connection.commit
return result
Example:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM customers WHERE address = %s"
adr = ("Yellow Garden 2", )
mycursor.execute(sql, adr)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
I used the following code to get items from sqlite3 database
def get(self, item_name, attrs=True): #get attr from item and return as dict, if attr==True: get all items
conn = self.conn
if attrs: #all
return conn.execute('SELECT * FROM %s WHERE __item_key__ = "%s";' %(self.table, item_name))
else:
command = 'SELECT '
for attr in attrs:
command+= attr+' '
command+='FROM %s WHERE __item_key__ = "%s";' %(self.table, item_name)
return conn.execute(command)
print(get('name1'))
the code print the following:
<sqlite3.Cursor at 0x213d4c0f490>
instead of the values from the table.
When I try this:
get('name1')[0]
it returns:
TypeError: 'sqlite3.Cursor' object is not subscriptable
Full code:
import sqlite3 as sql
import sqlite3 as sql
class db:
'''
This class turns dicts into sqlite databases
and output sqlite databases as dicts
'''
def __init__(self, db_name, table_name): #open or create a database
conn = sql.connect(db_name).cursor()
self.table = table_name
self.conn = conn
def create(self, table_name, cols):
command = "CREATE TABLE %s(_item_key_ TEXT," % table_name
for key, value in cols.items():
command+="%s %s," %(key, value)
command=command[:-1]
command+=");"
self.conn.execute(command)
self.table = table_name
def get(self, item_name, attrs=True): #get attr from item and return as dict, if attr==True: get all items
conn = self.conn
if attrs: #all
return conn.execute('SELECT * FROM %s WHERE _item_key_ = "%s";' %(self.table, item_name))
else:
command = 'SELECT '
for attr in attrs:
if type(attr) == str:
attr = '"'+attr+'"'
command+= str(attr)+' '
command+='FROM %s WHERE _item_key_ = "%s";' %(self.table, item_name)
return conn.execute(command).fetchall()
def change(self, item_name, attrs): #change certain attrs of item
command = 'UPDATE %s SET ' %self.table
for key, value in attrs:
command += '%s=%s,'%(key, value)
command = command[:-1]+' WHERE _item_name_ = "'+item_name+'";'
def add(self, item_name, attrs): #add an item with attrs to database
command = 'INSERT INTO %s VALUES ("%s",' %(self.table, item_name)
for attr in attrs:
if type(attr) == str:
attr = '"'+attr+'"'
command += str(attr)+','
command = command[:-1]+');'
#print(command)
self.conn.execute(command)
def close(self): #close database
self.conn.close()
The table is supposed to look like the following (although I never saw it):
__item_name__ A B
---------------------------
'name1' 123 'hi'
'name2' 344 'bye'
Does anyone know how this works?
edit: I realized some bugs in create() and add(). However, after fixing some stuff it still prints the same thing in get().
It returns that no cursor object found.
If you want to get the results you need to add these lines:
cur = conn.cursor() # create a cursor to your connection
cur.execute(your_query) # execute your query
results = cur.fetchall() # fetch the results
Also don't forget to iterate over the cursor after results = cur.fetchall():
for row in results:
A = row[0]
B = row[1]
Should revise all code and implement this self.conn.commit() after self.conn.execute(---).
self.conn.execute(command)
self.conn.commit() #<--- THIS NEW line, to after .execute()
self.table = table_name
I have a database named products in sql and i wish to get all the rows as a dictionary or json. I've seen an example here but how do i pass username, password and host?
This is the example:
import json
import psycopg2
def db(database_name='products'):
return psycopg2.connect(database=database_name)
def query_db(query, args=(), one=False):
cur = db().cursor()
cur.execute(query, args)
r = [dict((cur.description[i][0], value) for i, value in enumerate(row)) for row in cur.fetchall()]
cur.connection.close()
return (r[0] if r else None) if one else r
my_query = query_db("SELECT * FROM main_prod WHERE id = 1")
print(my_query)
json_output = json.dumps(my_query)
print(json_output)
When i use it like this i'm getting this error:
File "/home/alex/Documents/Proiecte/Python/bapp/venv/lib/python3.5/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: fe_sendauth: no password supplied
When i'm doing like this
import json
import psycopg2
def db(database_name='products', password='...', host='123.123.123.13', user='alex'):
return psycopg2.connect(database=database_name, password=password, host=host, user=user)
def query_db(query, args=(), one=False):
cur = db().cursor()
cur.execute(query, args)
r = [dict((cur.description[i][0], value) for i, value in enumerate(row)) for row in cur.fetchall()]
cur.connection.close()
return (r[0] if r else None) if one else r
my_query = query_db("SELECT * FROM main_prod WHERE id = 1")
print(my_query)
json_output = json.dumps(my_query)
print(json_output)
It won't print anything, it just remains like in sleep.
How can i do it?
Try this:
import psycopg2
import json
def main():
conn_string = "database_name='products', password='...', host='123.123.123.13', user='alex'"
# print the connection string we will use to connect
print "Connecting to database\n ->%s" % (conn_string)
# get a connection, if a connect cannot be made an exception will be raised here
conn = psycopg2.connect(conn_string)
# conn.cursor will return a cursor object, you can use this cursor to perform queries
cursor = conn.cursor()
# execute our Query
cursor.execute("SELECT * FROM main_prod WHERE id = 1")
# retrieve the records from the database
records = cursor.fetchall()
objects = [
{
'id': row.id,
} for row in records
] # there you tell what data you want to return
json_output = json.dumps(objects)
print(json_output)
if __name__ == "__main__":
main()
I'm learning python since last few weeks. For better learning, I decided to work on some project. So here is my Class for MySQL connection and demo example as well. Can you please tell me. What other improvement can be possible for following code?
Structure?
What else I can do to optimize code?
And Please forgive. If I'm doing some silly mistakes in code. (I'm learning)
#!/usr/bin/python
import pymysql
# select (table, parameter)
# insert (table, data)
# update (table, id, data)
# delete (table, id)
class MySQL:
def __init__(self):
self.sort_by = ""
self.order = ""
# initiate database connection.
self.connection = pymysql.connect(host='localhost',
user='root',
password='',
db='sherlock',
charset='utf8mb4')
self.cursor = self.connection.cursor(pymysql.cursors.DictCursor)
# this function is for selecting any feild on any table.(feilds veriable is optinal)
def select(self, table, *feilds):
flds = "" #differnt name for feilds veriable.
if not feilds:
flds = '*'
else:
for f in feilds:
if not flds:
flds = f
else:
flds += ",`%s`" % f
sql = "SELECT %s FROM `%s` " % (flds, table)
if self.sort_by:
sql = sql +"order by "+ str(self.sort_by) +" "+ str(self.order)
print sql
self.cursor.execute(sql)
result = self.cursor.fetchall()
return result
# This function is for data sorting for Mysql; but optinal.
# example : SELECT * FROM `users` order by id asc
def order_by(self, sort_by="", order="", *args, **kwargs):
self.sort_by = sort_by
self.order = order
# this function is for closing Mysql connection
def close(self):
self.connection.close()
########### END OF MySQL CLASS #############
sql = MySQL()
# sql.order_by function should be called before the sql.select() function.
sql.order_by("email")
# this will select all the feilds from `users` table.
# you can specify whichever feilds you want to return. like : sql.select("users", "id, email")
result = sql.select("users", "password")
for email in result:
print email["password"]
sql.close()
I have a class as below that I'm using to connect to a remote SQL server instance from a linux server python web app. I define and set cursor in the init constructor and wish to use it throughout the class. How do I do this? I come form a java background and don't understand the scope and protection levels of Python fields.
import pyodbc
class SQLSeverConnection():
def __init__(self, DSN, user, password, database):
connectionString = 'DSN=%s;UID=%s;PWD=%s;DATABASE=%s;' % (DSN, user, password, database)
connection = pyodbc.connect(connectionString)
cursor = connection.cursor()
def getColumnData(self, columnName, tableName):
cursor.execute('SELECT ' columnName ' FROM ' tableName ' ORDER BY timestamp')
data = cursor.fetchall()
return data
def getColumnTitles(self, tableName):
cursor.execute('select column_name,* from information_schema.columns where table_name = 'tableName' order by ordinal_position')
columns = cursor.fetchall()
return columns
def getTableNames(self):
cursor.execute('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = ''BASE TABLE''')
tables = cursor.fetchall()
return tables
The answer is simple: Python's "methods" are really plain functions, and local variables are plain local variables. To set / access instance attributes, you must use the current instance, which is passed as first argument to the function (and by convention named self):
class SQLSeverConnection():
def __init__(self, DSN, user, password, database):
connectionString = 'DSN=%s;UID=%s;PWD=%s;DATABASE=%s;' % (DSN, user, password, database)
self.connection = pyodbc.connect(connectionString)
self.cursor = connection.cursor()
def getColumnData(self, columnName, tableName):
self.cursor.execute('SELECT ' columnName ' FROM ' tableName ' ORDER BY timestamp')
data = self.cursor.fetchall()
return data
def getColumnTitles(self, tableName):
self.cursor.execute('select column_name,* from information_schema.columns where table_name = 'tableName' order by ordinal_position')
columns = self.cursor.fetchall()
return columns
def getTableNames(self):
BASE_TABLE ='BASE_TABLE'
self.cursor.execute('SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'')
tables = self.cursor.fetchall()
return tables
Now using a single shared cursor for all operations is brittle, you'd better instanciate a new cursor for each operation. Also, since a cursor is an iterable, you may want to return the cursor itself and let client code iterate over it, it might save some memory...
class SQLSeverConnection(object):
def __init__(self, DSN, user, password, database):
connectionString = 'DSN=%s;UID=%s;PWD=%s;DATABASE=%s;' % (DSN, user, password, database)
self.connection = pyodbc.connect(connectionString)
def getCursor(self):
return self.connection.cursor()
def getColumnData(self, columnName, tableName):
cursor = self.getCursor()
cursor.execute('SELECT ' columnName ' FROM ' tableName ' ORDER BY timestamp')
return cursor
# etc
Oh and yes: using mixCased is not pythonic, we prefer all_lower ;)
change cursor to self.cursor
def __init__(self, DSN, user, password, database):
connectionString = 'DSN=%s;UID=%s;PWD=%s;DATABASE=%s;' % (DSN, user, password, database)
connection = pyodbc.connect(connectionString)
self.cursor = connection.cursor()
def getColumnData(self, columnName, tableName):
self.cursor.execute('SELECT ' columnName ' FROM ' tableName ' ORDER BY timestamp')
data = self.cursor.fetchall()
return data