Compare the columns in the database with the variables obtained in the python code










0














I have a MySQL database using Python(mysql.connector). This is my table:



+----+-------+-------+--------+------------+
| id | num1 | num2 | gen | filename |
+----+-------+-------+--------+------------+
| 1 | 45 | 55 | woman | vid1.mp4 |
| 2 | 25 | 35 | man | vid2.mp4 |
| 3 | 45 | 55 | man | vid3.mp4 |
| 4 | 5 | 15 | woman | vid4.mp4 |


I then get the list below by querying the database:



 [(1, 45, 55, 'woman', 'vid1.mp4'), (2, 25, 35, 'man', 'vid2.mp4'),(3, 45, 55, 'man', 'vid3.mp4'),(4, 5, 15, 'woman', 'vid4.mp4')]


I get the number and gender in my code and compare it with the num1, num2 and gender entered in the database.
For example:



if 25<num<35 and gender =='man':
filename = 'vid2.mp4'


How could I implement this?










share|improve this question



















  • 1




    It's not clear what you want. Are you selecting for filename? Are you trying to do other things? Please give an example of the output.
    – Rocky Li
    Nov 12 '18 at 22:11










  • yes i want to select filename based on num and gender.
    – dafi mock
    Nov 12 '18 at 22:12















0














I have a MySQL database using Python(mysql.connector). This is my table:



+----+-------+-------+--------+------------+
| id | num1 | num2 | gen | filename |
+----+-------+-------+--------+------------+
| 1 | 45 | 55 | woman | vid1.mp4 |
| 2 | 25 | 35 | man | vid2.mp4 |
| 3 | 45 | 55 | man | vid3.mp4 |
| 4 | 5 | 15 | woman | vid4.mp4 |


I then get the list below by querying the database:



 [(1, 45, 55, 'woman', 'vid1.mp4'), (2, 25, 35, 'man', 'vid2.mp4'),(3, 45, 55, 'man', 'vid3.mp4'),(4, 5, 15, 'woman', 'vid4.mp4')]


I get the number and gender in my code and compare it with the num1, num2 and gender entered in the database.
For example:



if 25<num<35 and gender =='man':
filename = 'vid2.mp4'


How could I implement this?










share|improve this question



















  • 1




    It's not clear what you want. Are you selecting for filename? Are you trying to do other things? Please give an example of the output.
    – Rocky Li
    Nov 12 '18 at 22:11










  • yes i want to select filename based on num and gender.
    – dafi mock
    Nov 12 '18 at 22:12













0












0








0







I have a MySQL database using Python(mysql.connector). This is my table:



+----+-------+-------+--------+------------+
| id | num1 | num2 | gen | filename |
+----+-------+-------+--------+------------+
| 1 | 45 | 55 | woman | vid1.mp4 |
| 2 | 25 | 35 | man | vid2.mp4 |
| 3 | 45 | 55 | man | vid3.mp4 |
| 4 | 5 | 15 | woman | vid4.mp4 |


I then get the list below by querying the database:



 [(1, 45, 55, 'woman', 'vid1.mp4'), (2, 25, 35, 'man', 'vid2.mp4'),(3, 45, 55, 'man', 'vid3.mp4'),(4, 5, 15, 'woman', 'vid4.mp4')]


I get the number and gender in my code and compare it with the num1, num2 and gender entered in the database.
For example:



if 25<num<35 and gender =='man':
filename = 'vid2.mp4'


How could I implement this?










share|improve this question















I have a MySQL database using Python(mysql.connector). This is my table:



+----+-------+-------+--------+------------+
| id | num1 | num2 | gen | filename |
+----+-------+-------+--------+------------+
| 1 | 45 | 55 | woman | vid1.mp4 |
| 2 | 25 | 35 | man | vid2.mp4 |
| 3 | 45 | 55 | man | vid3.mp4 |
| 4 | 5 | 15 | woman | vid4.mp4 |


I then get the list below by querying the database:



 [(1, 45, 55, 'woman', 'vid1.mp4'), (2, 25, 35, 'man', 'vid2.mp4'),(3, 45, 55, 'man', 'vid3.mp4'),(4, 5, 15, 'woman', 'vid4.mp4')]


I get the number and gender in my code and compare it with the num1, num2 and gender entered in the database.
For example:



if 25<num<35 and gender =='man':
filename = 'vid2.mp4'


How could I implement this?







python mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 2:45









Trooper Z

763424




763424










asked Nov 12 '18 at 22:02









dafi mock

245




245







  • 1




    It's not clear what you want. Are you selecting for filename? Are you trying to do other things? Please give an example of the output.
    – Rocky Li
    Nov 12 '18 at 22:11










  • yes i want to select filename based on num and gender.
    – dafi mock
    Nov 12 '18 at 22:12












  • 1




    It's not clear what you want. Are you selecting for filename? Are you trying to do other things? Please give an example of the output.
    – Rocky Li
    Nov 12 '18 at 22:11










  • yes i want to select filename based on num and gender.
    – dafi mock
    Nov 12 '18 at 22:12







1




1




It's not clear what you want. Are you selecting for filename? Are you trying to do other things? Please give an example of the output.
– Rocky Li
Nov 12 '18 at 22:11




It's not clear what you want. Are you selecting for filename? Are you trying to do other things? Please give an example of the output.
– Rocky Li
Nov 12 '18 at 22:11












yes i want to select filename based on num and gender.
– dafi mock
Nov 12 '18 at 22:12




yes i want to select filename based on num and gender.
– dafi mock
Nov 12 '18 at 22:12












1 Answer
1






active

oldest

votes


















0














When I have a list of data like you have specified here, it is useful to add column names to it.



data = [(1, 45, 55, 'woman', 'vid1.mp4') ... ] # This comes from DB
col_names = ['id','num1','num2','gen','filename']


Next, iterate over your data in a loop



for row in data:
# row now contains a single value, for instance (1, 45, 55, 'woman', 'vid1.mp4')

row_dict = dict(zip(col_names, row))
# row_dict is now a dictionary of 'id':1, 'num1':45 ...

if row_dict['num1'] == 25:
print("%s has an num1 of 25!" % row_dict)


I hope that this gives you an idea how to iterate over your data and detect certain values are as you expect.



Another options is to use tuple unpacking in the loop



for id, num1, num2, gen, filename in data:
if num1 == 25:
print ("wow!")


Based on your edit:



for id, num1, num2, gen, db_filename in data:
if 25<num1<35 and gen == 'man':
filename = db_filename
else:
print("Oh no, file not found")
filename = None





share|improve this answer






















  • thanks,but when i print(row_dict) then: 'id': (1, 45, 55, 'woman', 'vid1.mp4'), 'num1': (2, 25, 35, 'man', 'vid2.mp4'), 'gen': (4, 5, 15, 'woman', 'vid4.mp4'), 'num2': (3, 45, 55, 'man', 'vid3.mp4')
    – dafi mock
    Nov 12 '18 at 22:20










  • Whoops! @dafimock - I should have written dict(zip(col_names, row)) not ... ,data)). I have edited my example above to resolve this issue.
    – Tom Leys
    Nov 14 '18 at 0:02











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270752%2fcompare-the-columns-in-the-database-with-the-variables-obtained-in-the-python-co%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














When I have a list of data like you have specified here, it is useful to add column names to it.



data = [(1, 45, 55, 'woman', 'vid1.mp4') ... ] # This comes from DB
col_names = ['id','num1','num2','gen','filename']


Next, iterate over your data in a loop



for row in data:
# row now contains a single value, for instance (1, 45, 55, 'woman', 'vid1.mp4')

row_dict = dict(zip(col_names, row))
# row_dict is now a dictionary of 'id':1, 'num1':45 ...

if row_dict['num1'] == 25:
print("%s has an num1 of 25!" % row_dict)


I hope that this gives you an idea how to iterate over your data and detect certain values are as you expect.



Another options is to use tuple unpacking in the loop



for id, num1, num2, gen, filename in data:
if num1 == 25:
print ("wow!")


Based on your edit:



for id, num1, num2, gen, db_filename in data:
if 25<num1<35 and gen == 'man':
filename = db_filename
else:
print("Oh no, file not found")
filename = None





share|improve this answer






















  • thanks,but when i print(row_dict) then: 'id': (1, 45, 55, 'woman', 'vid1.mp4'), 'num1': (2, 25, 35, 'man', 'vid2.mp4'), 'gen': (4, 5, 15, 'woman', 'vid4.mp4'), 'num2': (3, 45, 55, 'man', 'vid3.mp4')
    – dafi mock
    Nov 12 '18 at 22:20










  • Whoops! @dafimock - I should have written dict(zip(col_names, row)) not ... ,data)). I have edited my example above to resolve this issue.
    – Tom Leys
    Nov 14 '18 at 0:02
















0














When I have a list of data like you have specified here, it is useful to add column names to it.



data = [(1, 45, 55, 'woman', 'vid1.mp4') ... ] # This comes from DB
col_names = ['id','num1','num2','gen','filename']


Next, iterate over your data in a loop



for row in data:
# row now contains a single value, for instance (1, 45, 55, 'woman', 'vid1.mp4')

row_dict = dict(zip(col_names, row))
# row_dict is now a dictionary of 'id':1, 'num1':45 ...

if row_dict['num1'] == 25:
print("%s has an num1 of 25!" % row_dict)


I hope that this gives you an idea how to iterate over your data and detect certain values are as you expect.



Another options is to use tuple unpacking in the loop



for id, num1, num2, gen, filename in data:
if num1 == 25:
print ("wow!")


Based on your edit:



for id, num1, num2, gen, db_filename in data:
if 25<num1<35 and gen == 'man':
filename = db_filename
else:
print("Oh no, file not found")
filename = None





share|improve this answer






















  • thanks,but when i print(row_dict) then: 'id': (1, 45, 55, 'woman', 'vid1.mp4'), 'num1': (2, 25, 35, 'man', 'vid2.mp4'), 'gen': (4, 5, 15, 'woman', 'vid4.mp4'), 'num2': (3, 45, 55, 'man', 'vid3.mp4')
    – dafi mock
    Nov 12 '18 at 22:20










  • Whoops! @dafimock - I should have written dict(zip(col_names, row)) not ... ,data)). I have edited my example above to resolve this issue.
    – Tom Leys
    Nov 14 '18 at 0:02














0












0








0






When I have a list of data like you have specified here, it is useful to add column names to it.



data = [(1, 45, 55, 'woman', 'vid1.mp4') ... ] # This comes from DB
col_names = ['id','num1','num2','gen','filename']


Next, iterate over your data in a loop



for row in data:
# row now contains a single value, for instance (1, 45, 55, 'woman', 'vid1.mp4')

row_dict = dict(zip(col_names, row))
# row_dict is now a dictionary of 'id':1, 'num1':45 ...

if row_dict['num1'] == 25:
print("%s has an num1 of 25!" % row_dict)


I hope that this gives you an idea how to iterate over your data and detect certain values are as you expect.



Another options is to use tuple unpacking in the loop



for id, num1, num2, gen, filename in data:
if num1 == 25:
print ("wow!")


Based on your edit:



for id, num1, num2, gen, db_filename in data:
if 25<num1<35 and gen == 'man':
filename = db_filename
else:
print("Oh no, file not found")
filename = None





share|improve this answer














When I have a list of data like you have specified here, it is useful to add column names to it.



data = [(1, 45, 55, 'woman', 'vid1.mp4') ... ] # This comes from DB
col_names = ['id','num1','num2','gen','filename']


Next, iterate over your data in a loop



for row in data:
# row now contains a single value, for instance (1, 45, 55, 'woman', 'vid1.mp4')

row_dict = dict(zip(col_names, row))
# row_dict is now a dictionary of 'id':1, 'num1':45 ...

if row_dict['num1'] == 25:
print("%s has an num1 of 25!" % row_dict)


I hope that this gives you an idea how to iterate over your data and detect certain values are as you expect.



Another options is to use tuple unpacking in the loop



for id, num1, num2, gen, filename in data:
if num1 == 25:
print ("wow!")


Based on your edit:



for id, num1, num2, gen, db_filename in data:
if 25<num1<35 and gen == 'man':
filename = db_filename
else:
print("Oh no, file not found")
filename = None






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 0:01

























answered Nov 12 '18 at 22:14









Tom Leys

15.1k63459




15.1k63459











  • thanks,but when i print(row_dict) then: 'id': (1, 45, 55, 'woman', 'vid1.mp4'), 'num1': (2, 25, 35, 'man', 'vid2.mp4'), 'gen': (4, 5, 15, 'woman', 'vid4.mp4'), 'num2': (3, 45, 55, 'man', 'vid3.mp4')
    – dafi mock
    Nov 12 '18 at 22:20










  • Whoops! @dafimock - I should have written dict(zip(col_names, row)) not ... ,data)). I have edited my example above to resolve this issue.
    – Tom Leys
    Nov 14 '18 at 0:02

















  • thanks,but when i print(row_dict) then: 'id': (1, 45, 55, 'woman', 'vid1.mp4'), 'num1': (2, 25, 35, 'man', 'vid2.mp4'), 'gen': (4, 5, 15, 'woman', 'vid4.mp4'), 'num2': (3, 45, 55, 'man', 'vid3.mp4')
    – dafi mock
    Nov 12 '18 at 22:20










  • Whoops! @dafimock - I should have written dict(zip(col_names, row)) not ... ,data)). I have edited my example above to resolve this issue.
    – Tom Leys
    Nov 14 '18 at 0:02
















thanks,but when i print(row_dict) then: 'id': (1, 45, 55, 'woman', 'vid1.mp4'), 'num1': (2, 25, 35, 'man', 'vid2.mp4'), 'gen': (4, 5, 15, 'woman', 'vid4.mp4'), 'num2': (3, 45, 55, 'man', 'vid3.mp4')
– dafi mock
Nov 12 '18 at 22:20




thanks,but when i print(row_dict) then: 'id': (1, 45, 55, 'woman', 'vid1.mp4'), 'num1': (2, 25, 35, 'man', 'vid2.mp4'), 'gen': (4, 5, 15, 'woman', 'vid4.mp4'), 'num2': (3, 45, 55, 'man', 'vid3.mp4')
– dafi mock
Nov 12 '18 at 22:20












Whoops! @dafimock - I should have written dict(zip(col_names, row)) not ... ,data)). I have edited my example above to resolve this issue.
– Tom Leys
Nov 14 '18 at 0:02





Whoops! @dafimock - I should have written dict(zip(col_names, row)) not ... ,data)). I have edited my example above to resolve this issue.
– Tom Leys
Nov 14 '18 at 0:02


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270752%2fcompare-the-columns-in-the-database-with-the-variables-obtained-in-the-python-co%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto