- A+
所属分类:网站首页
本文通过python实现mysql数据库的简单查询
1.获取连接
- import MySQLdb
- try:
- conn = MySQLdb.connect(
- host = "127.0.0.1x",
- port = 3306,
- user = "root",
- passwd = "111111",
- db = "news",
- charset = 'utf8'
- )
- except MySQLdb.Error as e:
- print('Error: %s' % e)
2.获取数据
- cursor = conn.cursor()
- cursor.execute('SELECT * FROM `news` ORDER BY `created_at` DESC;')
- rest = cursor.fetchone()
- print(rest)
3.关闭连接
- conn.close()