I've got psycopg2 running and I can successfully query tables of my database. This is a working example of querying the table my_table:
import psycopg2
try:
conn_string="dbname='my_dbname' user='user' host='localhost' password='password'"
print "Connecting to database\n->%s" % (conn_string)
conn = psycopg2.connect(conn_string)
print "connection succeeded"
except:
print "no connection to db"
cur = conn.cursor()
try:
cur.execute(""" SELECT * from my_table; """)
records = cur.fetchall()
cur.close()
except:
print "Query not possible"
Question: How can I query a view, let it be called my_view, within the same database my_dbname?
The same way you'd query a table. From a SELECT point of view, a VIEW is the exact same thing as a TABLE:
cur.execute("SELECT * from my_view")
Note that you generally do not want to use a black except:. Catch a specific exception if you have to, but you are usually better off not catching the exception at all rather than block all feedback on errors as you've done here.
Related
I have an SQLite3 database that I want to add to with python, this is the code i have to add a row
def create_connection(db_file):
""" create a database connection to a SQLite database """
conn = None
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
def add_password(conn, data):
"""
Create an entry into the password database
"""
try:
sql = 'INSERT INTO passwords(added,username,password,website,email) VALUES(?,?,?,?,?)'
cur = conn.cursor()
cur.execute(sql, data)
print('done')
return cur.lastrowid
except Error as e:
print(e)
connection = create_connection('passwords.db')
data = (datetime.now(), 'SomeUsername', 'password123', 'stackoverflow.com', 'some#email.com')
add_password(connection, data)
When I run it prints done and ends, there are no errors. However, when I open the database to view the table, it has no entries.
If I open the database and run the same SQL code
INSERT INTO passwords(added,username,password,website,email)
VALUES('13-5-2020', 'SomeUsername', 'password123', 'stackoverflow.com', 'some#email.com')
it adds to the table. So it must be a problem with my python code. How do I get it to add?
Just make conn.commit() after executing query. It should work
I have this:
import pymysql
import pymysql.cursors
host = "localhost"
port=3306
user = "db"
password='pass'
db='test'
charset='utf8mb4'
cursorclass=pymysql.cursors.DictCursor
try:
connection= pymysql.connect(host=host,port=port,user=user,password=passw,db=db,charset=charset,cursorclass=cursorclass)
Executor=connection.cursor()
except Exception as e:
print(e)
sys.exit()
I tried using the pandas to_sql(), but it is replacing the values in the table with the latest one. I want to insert the values into the table using the Pandas, but I want to avoid the duplicate entries and if any then it should get passed.
It might be possible to pickle the dataframe, and insert it into a table under a column of type BLOB. If you go this way, you'd have to depickle the result returned by mysqld
EDIT: I see what you are trying to do now. Here is a possible solution. Let me know if it works!
# assume you have declared df and connection
records = df.to_dict(orient = 'records')
for record in records:
sql = "INSERT INTO mytable ({0}) \
VALUES ({1})".format(record.keys(), record.values())
curs = connection.cursor()
try:
curs.execute(sql)
curs.close()
except:
break #handle/research the error
I'm still using Flask-mysql.
I'm getting the database context (the mysql variable) just fine, and can query on the database / get results. It's only the insert that is not working: it's not complaining (throwing Exceptions). It returns True from the insert method.
This should be done inserting the record when it commits, but for some reason, as I watch the MySQL database with MySQL Workbench, nothing is getting inserted into the table (and it's not throwing exceptions from the insert method):
I'm passing in this to insertCmd:
"INSERT into user(username, password) VALUES ('test1','somepassword');"
I've checked the length of the column in the database, and copied the command into MySQL Workbench (where it successfully inserts the row into the table).
I'm at a loss. The examples I've seen all seem to follow this format, and I have a good database context. You can see other things I've tried in the comments.
def insert(mysql, insertCmd):
try:
#connection = mysql.get_db()
cursor = mysql.connect().cursor()
cursor.execute(insertCmd)
mysql.connect().commit()
#mysql.connect().commit
#connection.commit()
return True
except Exception as e:
print("Problem inserting into db: " + str(e))
return False
You need to keep a handle to the connection; you keep overriding it in your loop.
Here is a simplified example:
con = mysql.connect()
cursor = con.cursor()
def insert(mysql, insertCmd):
try:
cursor.execute(insertCmd)
con.commit()
return True
except Exception as e:
print("Problem inserting into db: " + str(e))
return False
If mysql is your connection, then you can just commit on that, directly:
def insert(mysql, insertCmd):
try:
cursor = mysql.cursor()
cursor.execute(insertCmd)
mysql.commit()
return True
except Exception as e:
print("Problem inserting into db: " + str(e))
return False
return False
Apparently, you MUST separate the connect and cursor, or it won't work.
To get the cursor, this will work: cursor = mysql.connect().cursor()
However, as Burchan Khalid so adeptly pointed out, any attempt after that to make a connection object in order to commit will wipe out the work you did using the cursor.
So, you have to do the following (no shortcuts):
connection = mysql.connect()
cursor = connection.cursor()
cursor.execute(insertCmd)
connection.commit()
I'm using python's builtin sqlite3 DB module.
While inserting objects to my DB tables, following sqlite exception raised:
"PRIMARY KEY must be unique"
As there are different insert methods for each object, I can't say for sure in which table does it failed:
import sqlite3
...
class SomeObject1:
....
def _insert_some_object1(self, db_object):
self._cursor.execute('insert into %s values (?,?,?)' % TABLE_NAME,
(db_oject.v1, db_object.v2, db_object_v3,))
Exception got caught in main() by except Exception as e:, so it's only info I've got.
I would want to know in which table insertion failed, value that failed, etc...
What's the right way to get the most info from sqlite exceptions?
Thanks
I think this really all depends on what you are using to connect to the database. Each module will display different errors.
I personally use sqlalchemy, and it gives you detailed errors. Here is an example to show what I mean (note: this is just an example, I personally do not support inline sql):
import sqlalchemy
connection = sqlalchemy.create_engine('sqlite:///mydb.db')
cursor = connection.connect()
query = "INSERT INTO my_table (id, name) values(1, 'test');"
cursor.execute(query)
And the error that is returned:
sqlalchemy.exc.IntegrityError: (IntegrityError) PRIMARY KEY must be unique "INSERT INTO my_table (id, name) values(1, 'test');" ()
As far as core sqlite3 module, I don't believe it will show the query that was executed. If you don't use a module such as sqlalchemy, then you will need to handle and show the error yourself. Take a look at this as example:
import sqlite3
def execute(query):
try:
conn = sqlite3.connect('mydb.db')
c = conn.cursor()
c.execute(query)
conn.commit()
except Exception as err:
print('Query Failed: %s\nError: %s' % (query, str(err)))
finally:
conn.close()
execute("INSERT INTO my_table (id, name) values(1, 'test');")
And the output on error:
Query Failed: INSERT INTO weapon (id, name) values(1, 'test');
Error: PRIMARY KEY must be unique
I have seen some code like (sqlite3 in python)
try:
conn = sqlite3.connect('mydb.db')
c = conn.cursor()
c.execute(query)
conn.commit()
except sqlite3.Error as err:
print('Sql error: %s' % (' '.join(err.args)))
print("Exception class is: ", err.__class__)
I'm having some troubles with this python method.
I don't have any problem getting the select results but when I've tried to execute the update I don't get any results.
I have tried to generate another cursor object, redefine the cursor, generate another connection, use a different sql query (without the use of the %s) and I didn't have any results.
If you could give me any help i would be really appreciate.
def getTarea():
conn = db.connect('url','user','pass','dbInstance')
with conn:
try:
cursor = conn.cursor(db.cursors.DictCursor)
sql = "SELECT CMD, ID_TAREA FROM TAREAS WHERE OBTENIDA = '0' AND DEVICE_ID = '1001' ORDER BY FECHA_TAREA DESC LIMIT 1"
cursor.execute(sql)
f.write(sql+"\n")
# fetch all of the rows from the query
data = cursor.fetchone()
# print the rows
f.write("CMD: "+data["CMD"]+"\n")
f.write("ID_TAREA: "+ str(data["ID_TAREA"])+"\n")
idTarea = str(data["ID_TAREA"])
obtenido = 1
cursor.execute("""UPDATE TAREAS SET OBTENIDA=%s WHERE ID_TAREA =%s""", (obtenido, idTarea))
cursor.close()
conn.close()
except Exception as e:
f.write("error \n"+e)
return cmd
conn.commit() will commit the changes, as documented in this similar post: Database does not update automatically with MySQL and Python