I would like to know why all seems to be okay but nothing is insert in my mysql database ?
I get some data from an api and I would like to insert data in table.
Using flask, python, pymysql
Thanks in advance
api = Flask(__name__)
def connect():
db = pymysql.connect(database='rtap',port=3306, host='127.0.0.1', user='root', password='',ssl_ca="{ca-cert filename}", ssl_disabled=True)
log.basicConfig(level=log.DEBUG, format='%(asctime)s %(levelname)s:\n%(message)s\n')
print("Connexion réussie")
return db
#api.route('/')
def base():
return jsonify({"version":"1.0"})
#api.route('/azure', methods=['GET'])
def read():
db = connect()
log.info('Reading Datas')
plane = "SELECT * FROM `plane`;"
cursor = db.cursor()
cursor.execute(plane)
output = cursor.fetchall()
return jsonify(output)
#api.route('/azure', methods=['POST'])
def write():
db = connect()
cursor = db.cursor()
req = request.json["response"]
# cursor.executemany("INSERT INTO `plane` (aircraft_icao, reg_number) VALUES (%s, %s);", req)
# print(req)
key = []
for i in req:
try:
if (key==[]) :
plane = "INSERT INTO `plane` (aircraft_icao, reg_number) VALUES ('{}', '{}')".format(i["aircraft_icao"], i["reg_number"])
key.append(i["reg_number"])
else:
if(i["reg_number"] not in key) : #si la key n'a pas encore été utilisée, on peut ecrire la requette, sinon on ne fait rien
plane+= ", ('{}', '{}')".format(i["aircraft_icao"], i["reg_number"])
key.append(i["reg_number"])
except Exception as e:
print("failed")
# print(plane)
cursor.execute(plane)
return jsonify(req)
if __name__=='__main__':
api.run(debug=True, port=5000, host='0.0.0.0')
traceback
Assuming that the INSERT statement looked good at that debugging print function, then you probably need to commit the material to the DB after INSERT. Try adding
db.commit()
cursor.close()
after cursor.execute().
You need to commit your table changes by doing
db.commit()
immediately after each INSERT or sequence of INSERTs. Read this.
Related
I'm writing a python code to read from mysql database:
def create_server_connection(host, user, password):
connection = None
try:
connection = pymysql.connect(host='localhost',
user='root',
password='pwd',
database='raw_data',
cursorclass=pymysql.cursors.DictCursor)
print("MySQL Database connection successful")
except err as error:
print(f"Error: '{error}'")
return connection
def read_query(connection, query):
cur = connection.cursor()
result = None
try:
cur.execute(query)
result = cur.fetchall()
return result
except err as error:
print(f"Error: '{error}'")
return cur
def get_Tables_byName(cursor, tableName):
q1 = f'''
SELECT table_name FROM raw_data.tables
where table_name like '{tableName}'; '''
res = []
cursor.execute(q1)
for row in cursor:
res.append(row[0])
return res
get_Tables_byName(cursor,'data_31942010201')
If I want to call get_Tables_byName function, what should I put in the first parameter? If I put cursor, the error message shows NameError: name 'cursor' is not defined
I'm using MySQL in Python but my update function isn't updating the row and I can't understand the message error.
def atualizaCartelaTabela(campo,valor,jogador):
try:
connection = mysql.connector.connect(host='localhost',
user='root',
password='root',
database='modular')
cursor = connection.cursor()
sql = """UPDATE Cartela
SET %s = %s
WHERE PONTOS = %s"""
atualiza = (campo,valor,jogador)
cursor.execute(sql,atualiza)
except Error as e:
print("Erro ao atualizar campos da tabela Cartela ->", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
def atualizaCartelaTabela(campo,valor,jogador):
try:
connection = mysql.connector.connect(host='localhost',
user='root',
password='root',
database='modular')
cursor = connection.cursor()
sql = """UPDATE Cartela
SET %s = %s
WHERE PONTOS = %s"""
atualiza = (campo,valor,jogador)
cursor.execute(sql,atualiza)
except Error as e:
print("Erro ao atualizar campos da tabela Cartela ->", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
atualizaCartelaTabela('Um',2,'Jogador 1')
The error I'm getting:
1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Um' = 2
WHERE PONTOS = 'Jogador 1'' at line 2
And, this is the function used to create the table(working):
def criaTabelaCartela():
try:
connection = mysql.connector.connect(host='localhost',
user='root',
password='root',
database='modular')
cursor = connection.cursor()
cursor.execute("CREATE TABLE Cartela (PONTOS VARCHAR(10), Um int(2), Dois int(1), Tres int(1), Quatro int(1), Cinco int(1), Seis int(1), Full int(1), SequenciaBaixa int(1), Trinca int(1), Quadra int(1), SequenciaAlta int(1), Yahtzee int(1), PontuaçãoFinal int(1))")
connection.commit()
except Error as e:
print("Erro ao criar tabela Cartela ->", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
Looking at these 3 statements for what's causing the error:
sql = """UPDATE Cartela
SET %s = %s
WHERE PONTOS = %s"""
atualiza = (campo,valor,jogador)
cursor.execute(sql,atualiza)
When you put SET X = "y" in the update query, the column name X cannot be parameterised. Only the values.
So the column name for campo should be substituted directly in the SQL string, not during execute - but note that this is UNSAFE. Change the first parameter to the {} notation for format and leave the values as %s so that they are parameters for execute():
sql = """UPDATE Cartela
SET {} = %s
WHERE PONTOS = %s""".format(campo) # unsafe
atualiza = valor, jogador
cursor.execute(sql, atualiza)
As a safety check, make sure that the value of campo is the name of one of the columns of the table, except PONTOS or any of the import key columns. Better to check that it's in a white-list of column that you want to allow updating.
I am guessing the int - 2 is the problem here .
Try this out :
Make your SQL-query this :
sql = """
UPDATE Cartela
SET %s = CAST(%s AS int)
WHERE PONTOS = %s
"""
And where you're calling the method atualizaCartelaTabela make it this :
atualizaCartelaTabela('Um','2','Jogador 1')
I am generating dummy data in the database and then publishing to cloud.
As soon as it is published, I want to delete that entry from the database.
I understand that I need to send the 'mid' value in the publish method then call deleteFromDb() function. But mid is always 1, even though I return ret.mid=index. Index is the primary key retrieved from the database.
def on_publish(unused_client,unused_userdata,mid):
print('on_publish')
deleteFromDb(mid)
def main() :
TableSchema="""
create table if not exists heartbeat(
id integer primary key autoincrement,
Date_Time text,
Heartbeat text
);
"""
while True:
conn = sqlite3.connect(dbFile)
print("Connected to db")
conn.execute('pragma foreign_keys = on')
conn.commit()
curs = conn.cursor()
print "Writing to db..."
sqlite3.complete_statement(TableSchema)
curs.executescript(TableSchema)
conn.commit()
rectime=strftime("%Y-%m-%d %H:%M:%S", gmtime())
res="ON"
curs.execute("insert into heartbeat (Date_Time, Heartbeat)
values (?,?)",[rectime,res])
conn.commit()
print "Inserted Heartbeat Data into Database."
for row in curs.execute("select * from heartbeat"):
index=row[0]
continue
conn.commit()
encoded_row=''
encoded_row=json.dumps(row) #Dumped in the form of str
print encoded_row
client = mqtt.Client(client_id=_CLIENT_ID)
client.username_pw_set (username='unused', password=create_jwt(project_id, ssl_private_key_filepath, ssl_algorithm))
client=mqtt.Client()
client.on_connect = on_connect
client.on_publish=on_publish
client.on_message = on_message
client.tls_set(ca_certs=root_cert_filepath)
client.connect('mqtt.googleapis.com', 8883,60)
client.loop_start()
ret=client.publish(_MQTT_TOPIC,encoded_row,qos=1)
time.sleep(0.5)
ret.mid=index
client.loop_stop()
#print(ret.mid)
curs.close()
conn.close()
I am assuming you are using sqlite3 so you need to connect and execute a delete statement to delete and then commit() to make changes you saved. Try like this
import sqlite3
deleteFromDb(mid):
con=sqlite3.connect("mydatabase.db") #your db name
cursor=con.cursor()
cursor.execute("DELETE FROM TABLE_NAME WHERE ID = %s" % mid)
con.commit()
con.close()
Mind that I am new to flask and python for that matter, I appreciate any help that anyone gives. I'm looking to access one of the fields of my JSON response(just the field not the entire response), how should I go about parsing the response. Image of the response attached below,thanks.
This is my main thread
from flask import Flask,render_template,request
from Qhandler import Qhandler
from MakePlayer import MakePlayer
app = Flask(__name__)
#app.route('/createplayer',methods=['GET','POST'] )
def showCreatePlayer():
if request.method == 'POST':
MakePlayer(request.form['playername'],request.form['playerteam'],request.form['playerrole'], request.form['playerpos'])
return "created player: <br>"+request.form['playername']+" "+request.form['playerteam']+" "+request.form['playerrole']+" "+request.form['playerpos']
return render_template("createPlayer.html")
#app.route('/sucess')
def success():
return "success"
#app.route('/showplayers')
def showPlayers():
Q = Qhandler()
return Q.displayQuery(""" select * from Player""")
if __name__ == '__main__':
app.run(debug=True)
This is my query handler
from flask import Flask, jsonify, json
from flaskext.mysql import MySQL
class Qhandler(object):
#global mysql
global cursor
global connection
global mysql
# database connection
app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'Optimizer'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
def ins(self,query):
try:
connection=mysql.connect()
cursor = connection.cursor()
cursor.execute(query)
connection.commit()
except:
print "error running query"
finally:
#cursor.close()
connection.close()
def displayQuery(self,query):
try:
connection = mysql.connect()
cursor = connection.cursor()
cursor.execute(query)
fetchedData = cursor.fetchall()
fetchedData = jsonify(fetchedData)
#fetchedData = json.dumps(fetchedData)
#record = json.loads(fetchedData)
#print "the resonse is here:"
return fetchedData
except:
print "error running query"
finally:
#cursor.close()
connection.close()
current response is
screenshot of results
Use "fetchedData = json.dumps(fetchedData)" instead of "fetchedData = jsonify(fetchedData)" then create a json decoder and parse the response, refer to below :
def displayQuery(self,query):
try:
connection = mysql.connect()
cursor = connection.cursor()
cursor.execute(query)
fetchedData = cursor.fetchall()
fetchedData = json.dumps(fetchedData)
#create a json decoder
d = json.JSONDecoder()
fieldPlayerName = d.decode(fetchedData)
#parse the json that is returned ( fieldPlayerName[0][1])
print "should print the field with the player name",fieldPlayerName[0][1]
return fieldPlayerName[0][1]
I'm trying use Python and pyodbc to access SQL server 2008. The first connection works. Then, after the program finishes its job, it closes the connection. When the program tries to access the database and connect to it again, it fails in the statement:
self.conn = pyodbc.connect(DRIVER=self.DRIVER, SERVER=self.SERVER, DATABASE=self.DATABASE, UID=self.UID, PWD=self.PWD, charset="UTF-8")
but the first time is OK. So does anyone know why? Below is the Python code:
class ODBC_MS:
def __init__(self, DRIVER,SERVER, DATABASE, UID, PWD):
''' initialization '''
self.DRIVER = DRIVER
self.SERVER = SERVER
self.DATABASE = DATABASE
self.UID = UID
self.PWD = PWD
def _GetConnect(self):
''' Connect to the DB '''
if not self.DATABASE:
raise(NameError,"no getting db name")
try:
self.conn = pyodbc.connect(DRIVER=self.DRIVER, SERVER=self.SERVER,
DATABASE=self.DATABASE, UID=self.UID,
PWD=self.PWD, charset="UTF-8")
except Exception,e:
print e.message
else:
self.cur = self.conn.cursor()
if not self.cur:
raise(NameError,"connected failed!")
else:
return self.cur, self.conn
def ExecNoQuery(self,conn, cursor, sql):
cursor.execute(sql)
ret = conn.commit()
return ret
def _UnConnect(self,conn, cursor):
conn.close()
if __name__ == '__main__':
ms = ODBC_MS('{SQL SERVER}', r'<server>', '<db>', '<user>', '<password>')
cursor, conn = ms._GetConnect() #connection
sql = "create table XX for example"
ret = ms.ExecNoQuery(conn, cursor,sql) #sql operation
ms._UnConnect(conn, cursor) #close db
#access the database the second time.
ms = ODBC_MS('{SQL SERVER}', r'<server>', '<db>', '<user>', '<password>')
cursor, conn = ms._GetConnect() # not success, I don't know why
sql = "create table XX for example"
ret = ms.ExecNoQuery(conn, cursor,sql) #sql operation
ms._UnConnect(conn, cursor) #close db
The second time when the program calls ms.GetConnect(), the statement self.conn = pyodbc.connect(DRIVER=self.DRIVER, SERVER=self.SERVER, DATABASE=self.DATABASE, UID=self.UID, PWD=self.PWD, charset="UTF-8") fails.