首页 > 代码库 > Python MySql 数据库操作

Python MySql 数据库操作

# -*- coding: utf-8 -*-

# mysqlTest.py

import mysql.connector

conn = mysql.connector.connect(user='root',host='127.0.0.1', port='3306', password='admin', database='test_db', use_unicode=True)
cursor = conn.cursor()

# cursor.execute('insert into table_test(id, name) values(%s, %s)', ['4', 'hello wangwei'])
# cursor.rowcount

# conn.commit()
# cursor.close()

cursor = conn.cursor()
cursor.execute('select * from table_test')
values = cursor.fetchall()
print values

cursor.close()
conn.close()

Python MySql 数据库操作