Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python从txt读取并保存到mysql_Python_Mysql - Fatal编程技术网

Python从txt读取并保存到mysql

Python从txt读取并保存到mysql,python,mysql,Python,Mysql,我需要气象站的帮助。我想将所有结果保存到mysql数据库中,但目前我已将所有结果保存在txt文件中 你能帮我用python写一个脚本,从txt文件中读取并保存到mysql中吗 我的txt文件(temperature.txt)包含数据和温度。它看起来像: 2013-09-29 13:24 22.60 我正在使用python脚本从大的“result.txt”文件中获取温度和当前时间: 但我想将其“打印”到mysql表中。我知道如何连接,但我不知道如何将数据保存到表中 我知道我需要使用: #!/us

我需要气象站的帮助。我想将所有结果保存到mysql数据库中,但目前我已将所有结果保存在txt文件中

你能帮我用python写一个脚本,从txt文件中读取并保存到mysql中吗

我的txt文件(temperature.txt)包含数据和温度。它看起来像:

2013-09-29 13:24 22.60
我正在使用python脚本从大的“result.txt”文件中获取温度和当前时间:

但我想将其“打印”到mysql表中。我知道如何连接,但我不知道如何将数据保存到表中

我知道我需要使用: #!/usr/bin/python

import MySQLdb

# Open database connection
db = MySQLdb.connect("localhost","user","password","weather" )
我有我的数据库“天气”和表“温度”。不知道我是否制作了一个好的表(第一个是datatime,第二个是varchar(5))。现在我需要python脚本来读取这个文件并保存到mysql中

非常感谢您的支持。

下一步很简单:

from contextlib import closing

with closing(self.db.cursor()) as cur:
    cur.execute("INSERT INTO table1(`measured_at`,`temp`) VALUES(%s, %s)", (measured_at,  temp))
    self.db.commit()

另外,你问这个问题似乎是因为你没有做家庭作业,也没有阅读任何python教程如何使用MySQL

向我们展示您迄今为止所做的尝试以及您遇到的困难。我们不是基于一些模糊的需求为您编写代码。
from contextlib import closing

with closing(self.db.cursor()) as cur:
    cur.execute("INSERT INTO table1(`measured_at`,`temp`) VALUES(%s, %s)", (measured_at,  temp))
    self.db.commit()