import sqlite3
import traceback
from time import sleep
import mysql.connector
def check_user(user_id):
conn = mysql.connector.connect(host='localhost', database='online', user='root1', password='rootRRR111_')
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS online(id INT, last_online_date TEXT)')
conn.commit()
select = "SELECT * FROM online WHERE id = %s LIMIT 0, 1"
result = cur.execute(select, (user_id,))
if result is None:
insert = ('INSERT INTO online (id, last_online_date) VALUES (%s, %s)')
cur.reset()
cur.execute(insert, (user_id, online_time))
conn.commit()
def update_online_status(user_id, online_time):
conn = mysql.connector.connect(host='localhost', database='online', user='root1', password='rootRRR111_')
cursor = conn.cursor()
select = 'SELECT last_online_date FROM online WHERE id = %s'
result = cursor.execute(select, (user_id,))
old_online = result
online_time = f'{old_online},{online_time}'
cursor.reset()
cursor.execute('UPDATE online SET last_online_date = %s WHERE id = %s', (online_time, user_id))
conn.commit()
app = Client("my_account")
app.start()
while True:
try:
with open('ids.ini', 'r') as file:
users = file.read().splitlines()
for user in users:
result = app.get_users(user)
user_id = result['id']
if result['status'] == 'offline':
unix_timestamp = float(result['last_online_date'])
local_timezone = tzlocal.get_localzone()
local_time = datetime.fromtimestamp(unix_timestamp, local_timezone)
online_time = local_time.strftime("%Y/%m/%d %H:%M:%S")
elif result['status'] == 'online':
now = datetime.now()
online_time = now.strftime("%Y/%m/%d %H:%M:%S")
check_user(user_id)
update_online_status(user_id, online_time)
# sleep(300)
except Exception:
traceback.print_exc()
continue
app.stop()
I am writing a program that would read the online status of a user in telegram.
Instead of writing online to an existing user, a huge number of identical rows appear in the database.
Example:
Table with repetitions
When I try to fix something, there are a lot of errors.
mysql.connector.errors.programmingerror: not all parameters were used in the sql statement
mysql.connector.errors.internalerror: unread result found
and other...
Pls help!!
I have a function what is adding new records in to Mysql database after scanning a barcode. Function is working but there is a problem with returning result on the App screen.
When I scan the barcode first time it does not return any results even when i can see that the new record was created. But when i scan the barcode second time it returns only 1 record.
Edit:(added more about the issue)
And after that it is always -1 record, which would not be problem i could add +1 to all results but the problem is the first one, as it returns nothing at all.
I tried to use time.sleep(.3) between the queries but that did not have any effect.
I now wonder if the Python code is wrong or my SQL query should be somehow different.
def db_execute3(config, sql, val):
mydb = mysql.connector.connect(**config)
mycursor = mydb.cursor()
try:
mycursor.execute(sql, val)
mydb.commit()
except mysql.connector.Error as err:
if err.errno == errorcode.CR_CONN_HOST_ERROR:
popip.open()
Clock.schedule_once(popip.dismiss, 3)
elif err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
popx.open()
Clock.schedule_once(popx.dismiss, 3)
elif err.errno == errorcode.ER_BAD_DB_ERROR:
popdb.open()
Clock.schedule_once(popdb.dismiss, 3)
elif err.errno == errorcode.ER_NO_REFERENCED_ROW_2:
popbr.open()
Clock.schedule_once(popbr.dismiss, 3)
else:
mycursor.close()
def inbsort_btndwn(self, _):
cont = self.container_no.text.upper()
barc = self.sku_barcode.text.upper()
sort_worknumb = self.sort_worknumb.text.upper()
val = (sort_worknumb, cont, barc)
valx = (cont,barc)
if barc is "" and cont is "":
errorsound.play()
self.pallet_sku.text = ""
self.number_sku.text = ""
Clock.schedule_once(self.focus_container_no, 0.2)
elif barc is "" and cont is not "":
errorsound.play()
self.pallet_sku.text = ""
self.number_sku.text = ""
Clock.schedule_once(self.focus_sku_barcode, 0.2)
else:
try:
mydb = mysql.connector.connect(**config)
checkupd = mydb.cursor(prepared=True)
sqlq = "select * from inb_container where `container_no` = %s and `sku_code` = %s;"
checkupd.execute(sqlq, valx)
record = checkupd.fetchone()
if record is None:
errorsound.play()
popuni.content.text = "No records for scanned Barcode!"
popuni.open()
Clock.schedule_once(popuni.dismiss, 2)
Clock.schedule_once(self.clear_barcode, .2)
Clock.schedule_once(self.focus_sku_barcode, 0.21)
else:
correctsound.play()
sql = "INSERT INTO inb_sor_con (`work_number`, `container_no`,`sku_barcode`) VALUES (%s, %s, %s)"
db_execute3(config, sql, val)
sqlz = "SELECT ic.sort_box,ic.sort_pallet FROM inb_container ic, " \
"inb_sor_con ib WHERE ic.container_no =ib.container_no and ic.sku_code = ib.sku_barcode " \
"and ic.container_no = %s and ic.sku_code = %s"
valz = (cont, barc)
checkupd.execute(sqlz, valz) #v289-ukan1012044-n10 #msku8416005
myresult = checkupd.fetchall()
for row in myresult:
xxx = "Scanned: {} of: {}".format(checkupd.rowcount, row[0])
zzz = "{}".format(row[1])
self.pallet_sku.text = zzz
self.number_sku.text = xxx
Clock.schedule_once(self.clear_barcode, 0.2)
Clock.schedule_once(self.focus_sku_barcode, 0.21)
except mysql.connector.Error as err:
print(err.errno)
pass
This is the database:
create table inb_container(
`container_no` varchar(25) NOT NULL,
`sku_code` varchar(40) NOT NULL,
`sort_box` int(5),
`sort_pcs` int(6),
`sort_pallet` varchar(3),
Unique (container_no,sku_code)
);
create table inb_sor_con(
`id_scan` int(6) auto_increment not null primary key,
`work_number` varchar(12),
`container_no` varchar(25) NOT NULL,
`sku_barcode` varchar(40) NOT NULL,
`sort_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (work_number) REFERENCES user_logins(work_number),
FOREIGN KEY (container_no) REFERENCES inb_container(container_no));
As mentioned, I'd suggest refactoring your code so it's not mixing UI code with database access. That way you can more easily test each bit of your program (either manually, from a separate module) or automatically using unit tests or such.
You can also freely create new cursors, don't reuse a single one.
Here's one such refactoring...
def find_container(db, *, container_no, barcode):
cursor = db.cursor()
cursor.execute(
"select * from inb_container where `container_no` = %s and `sku_code` = %s",
(container_no, barcode),
)
return cursor.fetchone()
def insert_sor_con(db, *, sort_worknumb, container_no, barcode):
cursor = db.cursor()
cursor.execute(
"INSERT INTO inb_sor_con (`work_number`, `container_no`,`sku_barcode`) VALUES (%s, %s, %s)",
(sort_worknumb, container_no, barcode),
)
db.commit()
def get_scan_results(db, *, container_no, barcode):
cursor = db.cursor()
cursor.execute(
"""
SELECT ic.sort_box,ic.sort_pallet
FROM inb_container ic, inb_sor_con ib
WHERE (
ic.container_no = ib.container_no AND
ic.sku_code = ib.sku_barcode AND
ic.container_no = %s AND
ic.sku_code = %s
)""",
(container_no, barcode),
)
return cursor.fetchall()
def show_error(message):
errorsound.play()
popuni.content.text = message
popuni.open()
Clock.schedule_once(popuni.dismiss, 2)
class SomeUI:
def inbsort_btndwn(self, _):
container_no = self.container_no.text.upper()
barcode = self.sku_barcode.text.upper()
sort_worknumb = self.sort_worknumb.text.upper()
if not (barcode and container_no):
errorsound.play()
self.pallet_sku.text = ""
self.number_sku.text = ""
if not barcode:
Clock.schedule_once(self.focus_sku_barcode, 0.2)
else:
Clock.schedule_once(self.focus_container_no, 0.2)
return
try:
with mysql.connector.connect(**config) as mydb:
container_record = find_container(
mydb, container_no=container_no, barcode=barcode
)
if container_record:
correctsound.play()
insert_sor_con(
mydb,
sort_worknumb=sort_worknumb,
container_no=container_no,
barcode=barcode,
)
scan_results = list(
get_scan_results(mydb, container_no=container_no, barcode=barcode)
)
for sort_box, sort_pallet in scan_results:
self.pallet_sku.text = "{}".format(sort_pallet)
self.number_sku.text = "Scanned: {} of: {}".format(
len(scan_results), sort_box
)
else:
show_error("No records for scanned Barcode!")
except Exception as exc:
# may want to use `traceback.print_traceback()` here for more detail
print(exc)
show_error(f"Error: {exc}")
Clock.schedule_once(self.clear_barcode, 0.2)
Clock.schedule_once(self.focus_sku_barcode, 0.21)
I need to retrieve results from my sqlite3 database 160 rows at a time, and repeat that until there are no rows left for my query, this is what I have:
conn = sqlite3.connect("C:\\Users\\%s\\AppData\\Roaming\\GridcoinResearch\\reports\\Rain.db" % user_account)
c = conn.cursor()
conn.text_factory = str
address = c.execute('select Address from NNDATA where NeuralMagnitude != 0 and NeuralMagnitude is not null and CPID in (select cpids from GRIDCOINTEAM)').fetchmany(160)
conn.text_factory = float
nn_mag = c.execute('select NeuralMagnitude from NNDATA where NeuralMagnitude != 0 and NeuralMagnitude is not null and CPID in (select cpids from GRIDCOINTEAM)').fetchmany(160)
conn.close()
while True:
if nn_mag == ():
sys.exit("Complete")
The reason for sys.exit is I have a bunch of other code to go between conn.close() and while True:, so when the last loop is done I can exit the program. Right now its doing the first pass then the cmd.exe is hanging.
EDIT: Just relaised I dont tell the loop to select the NEXT 160, oh dear!
The fetchmany attribute returns an empty list if there is no item lefts so you can just check the validation of its result. Also note that you should remove the limit from your query and the fetchall. Because the whole essence of using fetchmany is to fetch limited results from your cursor object.
chunk_size = 160
while True:
result = nn_mag.fetchmany(chunk_size)
if not result:
sys.exit("Complete")
else:
# do something with result
OK the full answer is:
conn = sqlite3.connect("C:\\Users\\%s\\AppData\\Roaming\\GridcoinResearch\\reports\\Rain.db" % user_account)
c = conn.cursor()
position = 00
while True:
conn.text_factory = str
address_db = c.execute('select Address from NNDATA where NeuralMagnitude != 0 and NeuralMagnitude is not null and CPID in (select cpids from GRIDCOINTEAM) limit {}, 160'.format(position)).fetchall()
conn.text_factory = float
rac_db = c.execute('select rac from GRIDCOINTEAM where rac != 0 and rac is not null limit {}, 160'.format(position)).fetchall()
if not address_db:
conn.close()
sys.exit("Complete")
else:
position += 160
I am trying to make a very simple select using mysql connector , however , when i try to execute the following commands , the row always returns null , even if there is data to be selected
self.cnx = mysql.connector.connect(user='root', password='123qwe', host='192.168.56.1', database='teste')
self.cursor = self.cnx
self.cursor = self.cnx.cursor()
self.query = ("SELECT cod_produto,quantidade FROM itens_comanda WHERE cod_comanda = %s AND cod_produto = %s")
self.query_data = (comanda,item)
self.cursor.execute(self.query,self.query_data)
row = cursor.fetchone()
if row == None:
return [False,0,0]
else:
for i in row:
return [True,i[0],i[1]]
I am trying to run this small python script which inserts data to mySql database under MacOs however it gives me following error:
File "inserter.py", line 58, in
cursor.execute('SELECT countryId from searcher_country');
File "build/bdist.macosx-10.9-intel/egg/MySQLdb/cursors.py", line 205, in execute
File "build/bdist.macosx-10.9-intel/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
_mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now")
Script :
from openpyxl import load_workbook;
import MySQLdb;
import random;
connection = MySQLdb.connect(host='localhost',user='root',passwd='testpassword',db='test');
cursor = connection.cursor();
fileLoc = "data.xlsx";
wb = load_workbook(fileLoc);
ws = wb.active;
#outFile = raw_input("Where do you want your count to go? ");
countryCountProc = """ CREATE PROCEDURE countProc (OUT param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM searcher_country;
END;"""
readyFunction = """
CREATE FUNCTION ready(id INT)
returns CHAR(50)
return 'The program has been initialized';
"""
cursor.execute(countryCountProc);
cursor.execute(readyFunction);
outFile = '/tmp/testingCount';
print cursor.execute('CALL countProc(#a); SELECT #a INTO OUTFILE \'{0}\';'.format(outFile));
yearIndex = 2;
while True:
value = str(ws.cell(row=1,column=yearIndex).value);
try:
sql = 'INSERT INTO searcher_year (year) values (\'{0}\')'.format(value.encode("utf8"))
cursor.execute(sql);
except Exception as e:
print sql
print e
yearIndex = yearIndex + 1
if value == '2011':
print yearIndex-1;
break;
countryIndex = 2;
while True:
value = ws.cell(row=countryIndex,column=1).value.replace('\'','\\\'');
try:
sql = 'INSERT INTO searcher_country (country) values (\'{0}\')'.format(value.encode("utf8"))
cursor.execute(sql);
except Exception as e:
print sql
print e
countryIndex+=1
if value == "Saba":
print countryIndex-1;
break;
cursor.execute('SELECT countryId from searcher_country');
results = [int(item[0]) for item in cursor.fetchall()]
minCountryId = min(results);
maxCountryId = max(results);
cursor.execute('SELECT yearId from searcher_year');
results = [int(item[0]) for item in cursor.fetchall()]
minYearId = min(results);
maxYearId = max(results);
for i in xrange(500):
yearId = random.randint(350,370);
countryId = random.randint(3800,3820)
data = round(random.random()*10,2);
sql = 'INSERT INTO searcher_data (country_id,year_id,stat) values ({0},{1},\'{2}\')'.format(countryId,yearId,str(data))
cursor.execute(sql);
connection.execute('SELECT ready(1) INTO OUTFILE {0}'.format(outFile))
connection.commit();
cursor.close();
You are not releasing the cursor after query execution. Kindly include
connection.commit(); cursor.close(); after each execution stateement