Delete all duplicated rows except max value from python - python

I have a MySQL database, that I am using from my Discord python bot with AIOMySQL, but I see that by error the bot created duplicated rows with the same ID but updated the values, and that last it's what I wanted. One example of my duplicated rows:
duplicated rows
So now I want to delete all the duplicated rows, except the one with max XP.
I did a backup first, and then I was trying to save in a list all the IDs, except the ones that already are in the list. And then for every ID delete all except the max value. Like in this code:
await cur.execute("SELECT ID FROM USUARIOS;")
r = await cur.fetchall()
uslist = []
for a in r:
for b in a:
if b in uslist:
pass
elif b not in uslist:
uslist.append(b)
for user in uslist:
await cur.execute("SELECT * FROM USUARIOS WHERE ID = {} ORDER BY XP LIMIT 1;".format(user))
r = await cur.fetchone()
uid = r[0]
print(uid)
xp = r[1]
await cur.execute("DELETE FROM USUARIOS WHERE ID = {} and xp != {};".format(uid, xp))
await conn.commit()
But when I saw the DB some rows were completelely deleted, including the max values.

Assuming you want to do this in MySQL:
SELECT * FROM table WHERE XP <> (SELECT MAX(XP) FROM table) GROUP BY ID, XP, GC
UNION
SELECT * FROM table WHERE XP = (SELECT MAX(XP) FROM table)

Related

I want to fetch the latest id in flask mysql

When I run the following query, it gives me the first id, I want the last id and I want the data in that id, how can I do it?
cursor = mysql.connection.cursor()
sorgu = "Select * From site2"
result = cursor.execute(sorgu)
if result > 0:
articles = cursor.fetchone()
return render_template("site.html",articles = articles)
else:
return render_template("site.html")
First retrieve the max ID from the table (assuming your IDs are incrementing upward as rows are added. This means the largest is the last one), then use it in your final query
sorgu = "SELECT * FROM site2 WHERE ID = (SELECT MAX(ID) FROM site2)"
If you have a timestamp column in your data base you could do MAX(timestap) to get the latest ID as well

print the most common value from sql

I have a list of words in an SQLite database and I want to get the most common value and save it in a variable.I am using python3
here is how I got my most common value.
SELECT emotion,
COUNT(emotion) AS value_occurrence
FROM chatlog
GROUP BY emotion
ORDER BY value_occurrence DESC
LIMIT 1;
May be something like this?
#!/usr/bin/python
import sqlite3
conn = sqlite3.connect('yourdb')
cur = conn.cursor()
cur.execute('''SELECT emotion,
COUNT(emotion) AS value_occurrence
FROM chatlog
GROUP BY emotion
ORDER BY value_occurrence DESC
LIMIT 1''')
rows = cur.fetchall()
    for row in rows:
        x = row[0]
y = row[1]
print(x,y)

Python: MySQL DELETE issues

I am trying to delete a value from a table with 2 columns; twitch and name.
This is the code:
cur2 = db.cursor()
cur2.execute("SELECT twitch FROM Alan45wmzYGCc5C7TIMCdczrel.whitelist WHERE twitch NOT IN(SELECT twitch FROM Alan45wmzYGCc5C7TIMCdczrel.followers)")
result = cur2.fetchall()
db.commit()
for row in result:
unfollowers.extend(row[0])
unfollowers_join = '\n'.join(unfollowers)
print unfollowers_join
cur3 = db.cursor()
for x in follows:
cur3.execute("DELETE FROM Alan45wmzYGCc5C7TIMCdczrel.whitelist WHERE twitch = (%s);", (x,))
data_unfollower = (unfollowers_join)
db.commit()
The result is the value needing to be deleted, and the result is the cur2 query to find unmatched values.

Index out of range while executing results from db

I'm having a problem while trying to simply execute data from rows from db (sqlite3). The DB input has 4 fields, therefore once entered they're being saved. But here's my problem, where I execute all of the 4 rows, if one of the fields was not filled I get an error.
That's the database execute code:
def ids(self):
con = lite.connect('foo.db')
with con:
cur = con.cursor()
cur.execute("SELECT Id FROM foo")
while True:
ids = cur.fetchall()
if ids == None:
continue
return ids
And since there are 4 rows, my output code:
print ''.join(ids[0]) + ',' + ''.join(ids[1]) + ',' + ''.join(ids[2])
+ ',' + ''.join(ids[3])
so my question is how to make an exception when there's no existing row to not show anything and just leave the ones that actually exist? I tried doing if ids[0] is not None: #do something but that would make my code really slow and it's non-pythonic way I guess. Is there any better way to make that work? Any help will be appreciated.
You don't seem to have 4 rows. Make it generic and just join an arbitrary number of rows:
ids = someobject.ids()
print ','.join(''.join(row) for row in ids)
You can simplify your database query, there is no need to 'poll' the query:
def ids(self):
with lite.connect('foo.db') as con:
cur = con.cursor()
cur.execute("SELECT Id FROM foo")
return cur.fetchall()
You could also just loop directly over the cursor, the database will handle buffering as you fetch:
def ids(self):
with lite.connect('foo.db') as con:
cur = con.cursor()
cur.execute("SELECT Id FROM foo")
return cur # just the cursor, no fetching
ids = someobject.ids()
# this'll loop over the cursor, which yields rows as required
print ','.join(''.join(row) for row in ids)

while loop in python sql query

Hi i have try this query in php which is running fine and i have to do this same in python
$select=mysql_query("SELECT DISTINCT A.entity_id AS entity_id ,A.email AS email,A.catquizid AS style_quiz_score ,A.catquizquesans AS style_quiz_answer,A.created_at AS date_joined,A.is_active AS is_active ,B.attribute_id AS attribute_id,B.value AS info FROM `customer_entity` AS A inner join `customer_entity_varchar` AS B on A.entity_id=B.entity_id WHERE B.`attribute_id` IN (1,2) limit 10",$conn);
$arr=array();
while($result= mysql_fetch_assoc($select))
{
if(!isset($arr[$result['entity_id']]['lastname'])){
$arr[$result['entity_id']]['firstname'] = $result['info'];
}
$arr[$result['entity_id']]['lastname'] = $result['info'];
$arr[$result['entity_id']]["email"]=$result['email'];
$arr[$result['entity_id']]["style_quiz_score"]=$result['style_quiz_score'];
$arr[$result['entity_id']]["style_quiz_answer"]=$result['style_quiz_answer'];
$arr[$result['entity_id']]["date_joined"]=$result['date_joined'];
$arr[$result['entity_id']]["is_active"]=$result['is_active'];
$arr[$result['entity_id']]["username"]=normalize_str($result['email']);
}
and in python i have tried this
def customer_migrate(request):
cursor = connections['migration'].cursor()
cursor.execute("SELECT DISTINCT A.entity_id AS entity_id ,A.email AS email,A.catquizid AS style_quiz_score ,A.catquizquesans AS style_quiz_answer,A.created_at AS date_joined,A.is_active AS is_active ,B.attribute_id AS attribute_id,B.value AS info FROM customer_entity AS A inner join customer_entity_varchar AS B on A.entity_id=B.entity_id WHERE B.attribute_id limit 4 ")
row = cursor.fetchall()
how can i use the while loop in the python query ,
Use fetchone() or just iterate through the cursor:
row = cursor.fetchone()
while row is not None:
print row
row = cursor.fetchone()
or
for row in cursor:
print row

Categories