Python连接MySQL数据库并查找表信息
1.导入mysqldb包
import MySQLdb
如果你的PyCharm中没有MySQLdb,就从Setting-》Project Interpreter查找并下载
2.在MySQL中新建一个连接,取名为python ,再新建一个测试表,取名为examples
CREATE TABhttp://www.devze.comLE IF NOT EXISTS examples ( php id int(11) NOT NULL AUTO_INCREMENT, description varchar(45), PRIMARY KEY (id) ); INSERT INTO examples(description) VALUES ("Hello World"); INSERT INTO examples(description) VALUES ("MySQL Example"); INSERT INTO examples(description) VALUES ("Flask Examplpythone");
3.书写Python代码
import MySQLdb db = MySQLdb.connect(host="localhost", # your host user="root", # username passwd="root", # password db="python") # name of the database # Create 编程客栈a Cursor object to execute queries. cur = db.cursor() # Select data from table using SQL query. cur.execute("SELECT * FROM examples") # print the first and second columns for row in cur.fetchall() : print row[0],编程 " ", row[1]
到此这篇关于Python连接MySQL数据库并查找表信息 的文章就介绍到这了,更多相关Python连接MySQL并查找表信息 内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论