python迭代行逐个mysql字段(python iterate row one by one mysql field)

编程入门 行业动态 更新时间:2024-10-27 20:23:54
python迭代行逐个mysql字段(python iterate row one by one mysql field)

我正在尝试一个程序,其中我有'table = qwes'和'field1 = first,field2 = second“。我需要达到每一行,必须通过条件'如果一行有问候,那么它是真的其他假'。

我使用limit并且需要增加偏移量然后它必须循环返回下一行找到true或false。 同样,它应该结束,直到所有行都完成。

import MySQLdb db = MySQLdb.connect(host="localhost", # your host, usually localhost user="root", # your username passwd="mysql", # your password db="sakila") # name of the data base cursor = db.cursor() qas = 0 while(qas < 100) qas = qas + 1 posts = "select * from ques LIMIT 1 OFFSET %s " %(qas) cursor.execute(posts) db.commit() keywords=[] a='hello' for i in cursor_posts.fetchall(): keywords.append(i[0]) if a in keywords: print true else: print false raw_input("please press enter to continue")

请帮我修理我的程序; 我希望while循环执行,直到行可用。 由于我缺乏知识,我默认为100。

I am trying a program where I have 'table= qwes' and 'field1= first, field2= second". I need to attain each row once, which must pass through the condition 'if a row has got hello, then its true else false'.

I used limit and needed to increment offset and then it has to loop back for next row finding true or false. Likewise it should end till all the rows are finished.

import MySQLdb db = MySQLdb.connect(host="localhost", # your host, usually localhost user="root", # your username passwd="mysql", # your password db="sakila") # name of the data base cursor = db.cursor() qas = 0 while(qas < 100) qas = qas + 1 posts = "select * from ques LIMIT 1 OFFSET %s " %(qas) cursor.execute(posts) db.commit() keywords=[] a='hello' for i in cursor_posts.fetchall(): keywords.append(i[0]) if a in keywords: print true else: print false raw_input("please press enter to continue")

Please help me in fixing my program; I want the while loop to execute until are the rows available. Due to my lack of knowledge I made it for default as 100.

最满意答案

我的建议是做这样的事情:

import MySQLdb db = MySQLdb.connect(host="localhost", # your host, usually localhost user="root", # your username passwd="mysql", # your password db="sakila") # name of the data base cursor = db.cursor() posts = "select * from ques LIMIT 100" cursor.execute(posts) db.commit() a='hello' counter = 0 for j in cursor.fetchall(): counter += 1 if a in j[0]: print ("Row " + str(counter) + "= " + j[0] + " => True") else: print ("Row " + str(counter) + "= " + j[0] + " => False") raw_input("please press enter to continue")

基本上用100条记录查询数据库...然后遍历执行语句的fetchall()并检查'hello'是否在j [0]中,这是你的帖子所在...假设你的第一列数据库是帖子......如果它是一个id然后将它移动到j [1] ...只需在它上面运行一个调试,当你通过for循环时看看j ..应该是一个如果这不起作用,简单修复

my recommendation would be to do somehting like this:

import MySQLdb db = MySQLdb.connect(host="localhost", # your host, usually localhost user="root", # your username passwd="mysql", # your password db="sakila") # name of the data base cursor = db.cursor() posts = "select * from ques LIMIT 100" cursor.execute(posts) db.commit() a='hello' counter = 0 for j in cursor.fetchall(): counter += 1 if a in j[0]: print ("Row " + str(counter) + "= " + j[0] + " => True") else: print ("Row " + str(counter) + "= " + j[0] + " => False") raw_input("please press enter to continue")

basically query the database once with 100 records... then loop through the fetchall() of your executed statement and check if 'hello' is in j[0] which is where your post would be... assuming the first column in your database is the post... if its an id then move it to j[1]... just run a debug on it as it goes and look at what is in j when you go through the for loop.. should be a simple fix if this doesn't work

更多推荐

本文发布于:2023-07-14 20:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107335.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   迭代   mysql   python   row

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!