2016-11-17 85 views
1

我正在使用python腳本將文本文件導入到mysql數據庫,但我得到一個奇怪的錯誤並通過互聯網搜索它,但無法找到確切的解決方案。我想創建列和某些列將與負號保存在十進制數據這樣TypeError:在使用MySQL和Python進行字符串格式化時轉換的並非所有參數

Alarmdata fuAlarm 
-1585.4  -35.3 
-343.32  -54.3 

我收到以下錯誤

_mysql_exceptions.ProgrammingError: (1064, "You have an error in your 
SQL syntax; check the manual that corresponds to your MySQL server 
version for the right syntax to use near '     Alarmdata, 
fuel_in_l DECIMAL(5,2), fuAlarm DECIMAL(4,3),  ' at line 1") 

我通過給數據類型爲空白的數據,但現在解決了這個問題我正面臨這個錯誤。

TypeError: not all arguments converted during string formatting 

我的代碼如下

import os 
import sys 
import csv 
import MySQLdb as mdb 

con = mdb.connect('localhost' , 'root' , 'kami' , 'tempdat') 

cursor = con.cursor() 
cursor.execute('CREATE TABLE Datatmp (id INT NOT NULL PRIMARY KEY  AUTO_INCREMENT, \ 
      timestamp INT(12), pomode INT(3), modeAlarm INT(3), \ 
      temperature_C DECIMAL(2,1), temperatureAlarm INT(3), \ 
      switch INT(3), Powerswitch INT(3), SIM_dollar INT , \ 
      Alarmdata INT, fuel_in_l DECIMAL(5,2), fuAlarm DECIMAL(4,3), \ 
      next_maint DECIMAL(5,2), next_maintenanceAlarm INT(2))'); 

con.commit()    

file = open('//home/mysql/kami.txt', 'rb') 
creader = csv.reader(txtfile.readlines()[3:], delimiter='\t') 

for t in creader: 
    cursor.execute('INSERT INTO Datatmp(timestamp, pomode, modeAlarm,\ 
        temperature_C, temperatureAlarm, \ 
        switch, Powerswitch, SIM_dollar, \ 
        Alarmdata, fuel_in_l, fuAlarm \ 
        next_maint, next_maintenanceAlarm)\ 
        Values(%s,%s,%s,%s,%s,%s,NULL,NULL,%s,%s,%s,\ 
        %s,%s,%s,%s,%s,%s)', t) 

file.close() 
con.commit() 
con.close() 

類錯誤是說我沒有通過足夠%S,但我通過相同的號碼,它不應該顯示此錯誤。我是MySQL和python的新手,所以我沒有解決。

+0

在您的表格定義中,您沒有Alarmdata的數據類型 – Niagaradad

+0

@Niagaradad是的,那裏我不知道。如果該列在表中,但沒有數據,則只是空白。那麼我應該顯示哪種數據類型? NULL還是INT?第二,如果我有價值像-152.43,我該如何分配這種數據類型? 。如果有任何提示,我會非常感激。我花了整整一天的時間。 – Rio

+1

OP - 一個社區wiki被添加爲您最後一個陳述的答案,但沒有任何意義。 Python的* Nones *在MySQL中被翻譯爲* Nulls *。此外,您的附加查詢缺少逗號,並且傳入的列表字段的佔位符太多。未來的讀者可能會對你發現的內容感興趣。請使用示例代碼調整完全回答您自己的問題。 – Parfait

回答

0
import os 
import sys 
import csv 
import MySQLdb as mdb 


con = mdb.connect('localhost' , 'root' , 'kami' , 'tempdat') 

cursor = con.cursor() 
cursor.execute('CREATE TABLE Datatmp (timestamp INT(12), pomode INT(3), \ 
      modeAlarm INT(3), temperature_C DECIMAL(2,1),temperatureAlarm INT(3), \ 
      switch INT(3), Powerswitch INT(3),SIM_dollar INT(3), \ 
      Alarmdata INT(5),fuel_in_l DECIMAL(5,2), fuAlarm DECIMAL(4,3), \ 
      next_maint DECIMAL(5,2), next_maintenanceAlarm INT(2))'); 




con.commit()    

file = open('//home/mysql/kami.txt', 'rb') 
creader = csv.reader(txtfile.readlines()[3:], delimiter='\t') 



# now insert values in database 
i = 0; 

# read and store the values in database and also replace empty data 

for t in creader: 
    print t 
    for idx, v in enumerate(t): 
     if v == ' ': 
      t[idx] = None     
    cursor.execute('INSERT INTO Data_121_RAPI_txt VALUES(%s,%s,%s,%s,%s,%s, \ 
       %s,%s,%s,%s,%s,%s, %s)', t)     
i = i + 1 



file.close() 
con.commit() 
con.close() 

這裏我面臨兩個問題。 1)如果有像Alarmdata這樣的空列,我仍然需要傳遞數據類型,並且在這裏傳遞INT數據類型,因此可以很容易地刪除這個錯誤。

2) TypeError: not all arguments converted during string formatting  

這裏的問題是,我通過NULL在cursor.execute和它給錯誤,所以我做的代碼,把每一個空的空間,在裏面寫無。並且在palceholder中,我沒有提供NULL,而只是普通的palceholder,並且提供了正確數量的佔位符,問題就解決了。

我希望它能幫助別人。

0

在MySQL中,INT數據類型的值範圍可以從-21474836482147483647。所以你可以簡單地把數據類型INT放在那裏。休息一切將由MySQL照顧。

+0

你的意思是指定-153.33我會給INT的數據類型?因爲我通過分配INT數據類型來解決空白數據。雖然我得到一個新的錯誤。 TypeError:並非在字符串格式化過程中轉換的所有參數 但我很快就會解決它。實際的問題是將數據類型分配給-153.33數字。 – Rio

0

(發表於OP)

我解決了這個問題。問題是,如果我們將它們轉換爲None類型,則Python會讀取空格。

相關問題