我的Python环境:
Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
安装MySQL接口用的包:
pip install MySQL-python
直接在Python解释器下操作
Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='**********',db='firstSQL')
>>> cur=conn.cursor()
>>> reCount = cur.execute('select * from Products')
>>> print cur.fetchone()
('BR01', 'BRS01', '8 inch teddy bear', Decimal('5.99'), '8 inch teddy bear, comes with cap and jacket')
>>> print cur.fetchmany(8)
(('BR02', 'BRS01', '12 inch teddy bear', Decimal('8.99'), '12 inch teddy bear, comes with cap and jacket'), ('BR03', 'BRS01', '18 inch teddy bear', Decimal('11.99'), '18 inch teddy bear, comes with cap and jacket'), ('BNBG01', 'DLL01', 'Fish bean bag toy', Decimal('3.49'), 'Fish bean bag toy, complete with bean bag worms with which to feed it'), ('BNBG02', 'DLL01', 'Bird bean bag toy', Decimal('3.49'), 'Bird bean bag toy, eggs are not included'), ('BNBG03', 'DLL01', 'Rabbit bean bag toy', Decimal('3.49'), 'Rabbit bean bag toy, comes with bean bag carrots'), ('RGAN01', 'DLL01', 'Raggedy Ann', Decimal('4.99'), '18 inch Raggedy Ann doll'), ('RYL01', 'FNG01', 'King doll', Decimal('9.49'), '12 inch king doll with royal garments and crown'), ('RYL02', 'FNG01', 'Queen doll', Decimal('9.49'), '12 inch queen doll with royal garments and crown'))
>>> print cur.fetchall()
()
>>> cur.close()
>>> conn.close()
网友评论