简体   繁体   English

无法使用python在RaspberryPi中的MySQL表中保存数据

[英]Unable to Save Data in MySQL table in RaspberryPi using python

I am stuck in a very difficult problem of not able to store data in the table of MySQL installed in raspberryPi using Python. 我陷入了一个非常困难的问题,即无法使用Python将数据存储在raspberryPi中安装的MySQL表中。 I am just trying to save Hello World. 我只是想保存Hello World。 Please go through the program i have used and the results i am getting. 请仔细阅读我使用的程序和我得到的结果。 Please help where i am going wrong. 请帮助我出错的地方。 Thank you in advance. 先感谢您。

import MySQLdb 导入MySQLdb

try: 尝试:

db=MySQLdb.connect("localhost","root","int3rneT@","intruder") 分贝= MySQLdb.connect( “本地主机”, “根”, “int3rneT @”, “入侵者”)

print("Success") 打印(“成功”)

curs=db.cursor() 小人= db.cursor()

print("success1") 打印( “success1”)

with db: 与db:

z="HelloWorld" Z = “HelloWorld” 的

print(z) 打印(Z)

print("success2") 打印( “success2”)

curs.execute("""INSERT into PIR1 values (%s)""",(z)) curs.execute(“”“INSERT into PIR1 values(%s)”“”“,(z))

print("success3") 打印( “success3”)

db.commit() db.commit()

print("success4") 打印( “success4”)

except: 除了:

print("not connecting") 打印(“不连接”)


The result i am getting is : 我得到的结果是:

pi@raspberrypi:~ $ python3 testingDataBaseConnection.py pi @ raspberrypi:〜$ python3 testingDataBaseConnection.py

Success 成功

success1 success1

not connecting 不连接

From the result, i concluded that code is not running after "with db". 从结果中,我得出结论,在“with db”之后代码没有运行。


I have created table PIR1 as below: 我创建了表PIR1如下:

create TABLE PIR1(gpsData VARCHAR(200)); 创建TABLE PIR1(gpsData VARCHAR(200));

I am highly doubtful about the above line whether it is correct or not. 我对上述这条线是否正确非常怀疑。

When i run show tables command, i get this, which concludes the table is created. 当我运行show tables命令时,我得到了这个,结果表创建了表。

MariaDB [intruder]> show tables; MariaDB [入侵者]>显示表格;

+--------------------+ + -------------------- +

| | Tables_in_intruder | Tables_in_intruder |

+--------------------+ + -------------------- +

| | PIR1 | PIR1 |

+--------------------+ + -------------------- +

1 row in set (0.00 sec) 1排(0.00秒)

Perhaps try printing the exception to see what went wrong? 也许尝试打印异常,看看出了什么问题?

except Exception as error: 
    print(error)

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

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