Python連接MySQL數(shù)據(jù)庫并查找表信息
更新時間:2023年08月29日 09:44:05 作者:白芷加茯苓
本文主要介紹了Python連接MySQL數(shù)據(jù)庫并查找表信息,通過使用Python中的MySQL Connector模塊,連接到MySQL服務器并執(zhí)行SQL查詢語句,可以獲取表的結構、列信息、行數(shù)據(jù)等,感興趣的可以了解一下
1.導入MySQLdb包
import MySQLdb
如果你的PyCharm中沒有MySQLdb,就從Setting-》Project Interpreter查找并下載
2.在MySQL中新建一個連接,取名為python ,再新建一個測試表,取名為examples
CREATE TABLE IF NOT EXISTS examples ( 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 Example");
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數(shù)據(jù)庫并查找表信息 的文章就介紹到這了,更多相關Python連接MySQL并查找表信息 內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python 實現(xiàn)12bit灰度圖像映射到8bit顯示的方法
這篇文章主要介紹了python 實現(xiàn)12bit灰度圖像映射到8bit顯示的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07