1

I have a trouble with my program in my raspberry pi. This is my source code

import MySQLdb
import csv
db=MySQLdb.connect(user='root',passwd='toor',
                        host='127.0.0.1',db='data')
cursor=db.cursor()
csv_data=csv.reader(file('datasensor.txt'))
for row in csv_data:
        sql = "insert into `kelembapan` (`id`,`Tanggal`,`Tipe_sensor`,`Value`,`Ket`) values(%s,%s,%s,%s,%s);"
        cursor.execute(sql,row)
db.commit()
cursor.close()
print "The Data has been inputted"

and this is for txt file

1, 2017-10-10, sensor1,40,Kurang lembap
2, 2017-10-10, sensor2,60,Lembap

That program can run in my ubuntu but not in my raspberry. when run in raspberry there is error

Traceback (most recent call last):
 File "server.py", line 9, in <module>
  cursor.execute(sql,row)
 File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py",line 159, in execute
  query = query% db.literal(args)
TypeError: not enough arguments for format strings

Thnks before :)

OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
  • What happens when you try that same line of code using `.format`? [check this out](https://stackoverflow.com/questions/11146190/python-typeerror-not-enough-arguments-for-format-string) as it appears that way of doing it is being deprecated. – Jimmy Smith Jun 19 '17 at 15:03
  • Check your CSV file for non-matching rows (i.e. less than 5 elements) – zwer Jun 19 '17 at 15:04
  • @Jimmy Smith when im using .format, there is error NameError: name 'Tanggal' is not defined – Rahadian Wiratama Jun 19 '17 at 15:25
  • 1
    @zwer umm i have no idea. in my raspberry, my program isnt work. I'm using the same csv file in my ubuntu and raspberry – Rahadian Wiratama Jun 19 '17 at 15:30
  • Can you post the line where you're using format? We may just be missing some punctuation/separators. – Jimmy Smith Jun 19 '17 at 15:50
  • @JimmySmith sql = "insert into `kelembapan` values({0},'{1}',{2},{3},{4});" .format(id,Tanggal,Tipe_sensor,Value,Ket) – Rahadian Wiratama Jun 19 '17 at 15:55
  • Possible duplicate of [Python TypeError: not enough arguments for format string](https://stackoverflow.com/q/11146190/608639), [TypeError: not enough arguments for format string when using %s](https://stackoverflow.com/questions/24252358/typeerror-not-enough-arguments-for-format-string-when-using-s), [TypeError: not enough arguments for format string - using a While Loop](https://stackoverflow.com/q/13896612/608639), etc. – jww Jun 19 '17 at 17:50
  • @jww The error exists within `MySQLdb/cursors.py`, so the problem is before that – OneCricketeer Jun 20 '17 at 04:03
  • @cricket_007 yeah the problem with cursors.py. When I saw the source code cursors.py in my raspi and in my ubuntu, it's different. so do you have any tips? I already reinstalled my mysqldb but it doesnt work – Rahadian Wiratama Jun 20 '17 at 07:04
  • Reinstalled how? Pip? That's the only thing I can think of – OneCricketeer Jun 20 '17 at 11:19
  • @cricket_007 yes. and I even tried to copy the code cursor.py from my ubuntu to my raspi, but error, there is no compat module . the code is 'from MySQLdb.compat import unicode' – Rahadian Wiratama Jun 20 '17 at 19:09
  • I would try a different mysql library, then. SQLAlchemy for example, and you can add in pandas for handling your CSV – OneCricketeer Jun 20 '17 at 19:27
  • @cricket_007 Can you give me tutorial import csv using pandas? I've tried following this https://stackoverflow.com/questions/43453420/import-csv-to-database-using-sqlalchemy . but the data imported as column name not as data. – Rahadian Wiratama Jul 20 '17 at 10:46
  • As far as I know, Pandas requires you to define the headers of the columns. That is how you index the dataframe that's created. Please see http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html – OneCricketeer Jul 20 '17 at 16:44
  • 1
    @cricket_007 thanks cricket. now I use sqlalchemy with pandas and work in my raspi. thank you very much bro :) – Rahadian Wiratama Jul 23 '17 at 08:21
  • Cool. Feel free to answer yourself own question below with your updated code – OneCricketeer Jul 23 '17 at 15:06

0 Answers0