python连接mysql数据库
                        
                            时间:2021-07-01 10:21:17
                            帮助过:4人阅读
							                        
                     
                    
                    
                     import MySQLdb
 2 
 3 hostname = 
"10.20.10.111"
 4 username = 
"user"
 5 passwd = 
"123456"
 6 port = 3306
 7 conn = MySQLdb.connect(host = hostname,port = port,user = username,passwd = passwd) 
#此处注意要写上形参的名字,否则报错
 8 cur =
 conn.cursor()
 9 r1 = cur.execute(
"show databases")
10 r2 = cur.fetchmany(t1) 
#返回数据库名的元祖
11 cur.close()
12 conn.commit()
13 conn.close()
若只查找,则line 12不加也没影响,若需修改数据库,如修改表结构、删除数据等,则需要加上
python连接mysql数据库
标签: