简体   繁体   English

尝试向mysqldb输入大量数据

[英]Trying to input a lot of Data into mysqldb

#!/usr/bin/env python

from __future__ import print_function

import pymysql.cursors

Journal=open('J_15April.txt')

jrn=Journal.read()

Zeile=jrn.split('14.04.2015')

conn = pymysql.connect(host='localhost', port=3306, user='root',passwd='',db='daten', cursorclass=pymysql.cursors.DictCursor)


ean = int(row[0])
smr = char(row[1])
uhrzeit = char(row[2])


for line in Zeile:
cur = conn.cursor()
    if 'BOTTLE_ID' in line

        line2 = line.split(';')

        line3 = line2[0].strip(' BOTTLE_UNIQUE: BOTTLE_ID: 0')

        uhrzeit = (line3[:-4])

        if 'EAN' not in line:
            ean = 0

        else:
           ean = line2[7].strip('EAN: ')


        if ' SMR: 1;' in line:
            smr = ('gelesen')

        else:
            smr = ('nicht gelesen')

        cur.execute("INSERT INTO lga (ean, smr, uhrzeit) VALUES  ('%s','%s','%s')%(ean, smr, uhrzeit)")
        cur.fetchall()




cur.close()
conn.commit()
conn.close()

Hi guys, 嗨,大家好,

I'm currently trying to put a lot of data into a mysqldb. 我目前正在尝试将大量数据放入mysqldb。 I wrote two separate programs, one is for filtering the important stuff out of a very long .txt and the other one inserts them in the database. 我编写了两个单独的程序,一个用于从很长的.txt中过滤掉重要的内容,另一个则将它们插入数据库中。

The actual problem now occured when I tried combine those two program to automatically read and insert the data. 当我尝试结合这两个程序以自动读取和插入数据时,现在出现了实际问题。 I think the structure of the shown program may be a little chaotic and that's the main reason why it is not working. 我认为所示程序的结构可能有点混乱,这是其无法正常工作的主要原因。 I'm absolutely new to python and it was quite hard for me two make the two other programs work so please help me out. 我对python绝对是新手,对我来说很难使另外两个程序正常工作,所以请帮我。

If you are interested in the two single functions I can post them as well. 如果您对这两个功能感兴趣,我也可以发布它们。

greetings 问候

trbo TRBO

Traceback 追溯

So this would be the Traceback when i run the code: 所以这是我运行代码时的回溯:

Traceback (most recent call last): 追溯(最近一次通话):

File "C:\\Users\\tristan.bonn\\Desktop\\Python\\Verbindung mit mysql\\Tabelle mit Daten füllen.2.py", line 17, in row = cur.fetchall() 文件“ C:\\ Users \\ tristan.bonn \\ Desktop \\ Python \\ Verbindung mit mysql \\ Tabelle mit Datenfüllen.2.py”,行17,行= cur.fetchall()

File "C:\\Users\\tristan.bonn\\AppData\\Local\\Programs\\Python\\Python35\\lib\\site-packages\\pymysql\\cursors.py", line 256, in fetchall self._check_executed() 在fetchall self中的第256行,文件“ C:\\ Users \\ tristan.bonn \\ AppData \\ Local \\ Programs \\ Python \\ Python35 \\ lib \\ site-packages \\ pymysql \\ cursors.py”。_check_executed()

File "C:\\Users\\tristan.bonn\\AppData\\Local\\Programs\\Python\\Python35\\lib\\site-packages\\pymysql\\cursors.py", line 70, in _check_executed raise err.ProgrammingError("execute() first") pymysql.err.ProgrammingError: execute() first 文件“ C:\\ Users \\ tristan.bonn \\ AppData \\ Local \\ Programs \\ Python \\ Python35 \\ lib \\ site-packages \\ pymysql \\ cursors.py”,第70行,在_check_executed中引发err.ProgrammingError(“ execute()first”) )pymysql.err.ProgrammingError:首先执行()

Maybe to make everything more clear here are the two codes im currently trying to put together 也许要使一切更加清晰,这是我目前正在尝试将的两个代码放在一起

from __future__ import print_function

import pymysql.cursors


conn = pymysql.connect(host='localhost', port=3306, user='root',passwd='',db='daten', cursorclass=pymysql.cursors.DictCursor)

cur = conn.cursor()

cur.execute("INSERT INTO lga (ean, smr, uhrzeit) VALUES ('85479630','gelesen','17:40:34')")
row = cur.fetchall()



cur.close()
conn.commit()
conn.close()

This is the first code, which connects to the database. 这是第一个代码,它连接到数据库。

Journal=open('J_15April.txt')

jrn=Journal.read()

Zeile=jrn.split('14.04.2015')

x = len (Zeile)

for line in Zeile:
    if 'BOTTLE_ID' in line:

        line2 = line.split(';')

        line3 = line2[0].strip(' BOTTLE_UNIQUE: BOTTLE_ID: 0')

        print (line3[:-4])

        if 'EAN' not in line:
            print ('keine EAN Vorhanden/Fehler')

        else:
            print(line2[7].strip('EAN: '))

        if ' SMR: 1;' in line:
            print ('gelesen')



        else:
            print ('nicht gelesen')

And this one reads the important data out of my .txt file 这将从我的.txt文件中读取重要数据

greetings 问候

trbo TRBO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM