2016-04-03 111 views
-2

我想从MySQL数据库中获取数据到Python 2.7中。我正在使用Ubuntu 14.04,并且我通常将Python中的数据插入到MySQL中,但现在我想要从MySQL接收或获取一些数据到Python中。如何从MySQL表中获取数据到Python 2.7

这是我的MySQL数据库:

  • 数据库名称:projet
  • 表名:

    create table action(date_time varchar(50) not null,temperature varchar(50),humidity varchar(50)); 
    

    enter image description here

    action

,它是用这种方法创建

这是我的表action谁找到温度和湿度的两个值(ON_1,OFF_1,,OFF_2)从PHP网页插入。

此图片将解释什么,我说:

enter image description here

所以在第一行:yes=ON_1NO= OFF_1

,并在第二行:yes= ON_2NO = OFF_2。用户可以输入任何内容。

所以我需要把这个值变成Python。

+0

我不知道你有多少工作投入这一点。你使用哪个库? MySQLdb的? – Pullie

回答

0

1日安装MySQLdb模块或

sudo apt-get install python-mysqldb 

测试模块

import MySQLdb 
con = MySQLdb.connect(host='localhost', user=username, passwd=password, db=db_name) 
cur = con.cursor() 
cur.execute("SELECT * FROM YOUR_TABLE_NAME") 

# print all the first cell of all the rows 
for row in cur.fetchall(): 
    print row[0] 

con.close()