簡體   English   中英

在raspberrypi中安裝MySql並使用python3將數據放入表中

[英]Install MySql in raspberrypi and put a data into a table using python3

我正在解決一個問題,我在raspberrypi中獲取GPS數據。 由於不兼容問題,我只能使用python3。 基本上,我必須在raspberrypi的mysql服務器中將字符串打印到表的列中。 我坐在這個問題上,但我無法做到。 我知道,我問了很多,但這是真正的問題。 我試過這個鏈接: https//pymysql.readthedocs.io/en/latest/user/examples.html

我遵循的安裝步驟是:

sudo apt update sudo apt-get install python3-mysqldb sudo apt-get install mysql-server python3-mysqldb sudo mysql_secure_installation sudo mysqladmin -u root -p create intruder // intruder是數據庫名sudo mysql intruder -u root -p

mysql> CREATE TABLE gps(GPSData VARCHAR(200)NOT NULL);

我把代碼放在我嘗試的下面。 我得到的錯誤是

pymysql.err.InternalError:(1698,“訪問被拒絕用戶'root'@'localhost'”)我已經嘗試了很多,但似乎沒有任何效果。

在此先感謝。請幫助。

import pymysql.cursors

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='root',
                             password='passwd',
                             db='intruder',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

  with connection.cursor() as cursor:
    # Create a new record
    sql = "INSERT INTO `gps` ( `GPSData`) VALUES (%s)"
    cursor.execute(sql, ('HelloWorld'))


connection.commit()
finally:
    connection.close()

使用非root用戶並保存此用戶,以防您丟失密碼:

sudo mysql

create user gps@localhost IDENTIFIED BY 'securepassword';
GRANT ALL ON intruder.* TO gps@localhost;

然后使用:

 connection = pymysql.connect(host='localhost',
                         user='gps',
                         password='securepassword',
                         db='intruder', ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM