python writes data to mysql database, taking building wifi fingerprint database as an example

Based on the ubuntu system, you first need to install the mysql database. The main process is as follows:

sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install libmysqlclient-dev

A database password needs to be set during installation.

After the installation is successful, log in to the database:

mysql -uroot -p123456 (-u is followed by the user name, and -p is followed by the database password set during installation)

After successfully logging in to the database, you can use show databases; view the existing databases;

Use create database wifi; to create a new database named wifi;

use use wifi; enter the database;

This is using show tables; you can view the data tables in the wifi database;

You can use create table wifidb(…); to create a data table, as shown in the following figure:


Using python to write a database requires pymysql to be installed:

pip install pymysql

The specific program to write data to the database is as follows:

import pymysql

conn = pymysql.connect(db='wifi',host='localhost',user='root',passwd='123456') #The parameters are database name, server IP address, here is local, username, password

c = conn.cursor() #Create a cursor object

c.execute('insert into wifidb(BSSID,Strength,Location_id) values ​​(%s,%s,%s)',(bssid[j],strength[j],i)) #Write data to wifidb data table

conn.commit() #Submit

c.close() #Close

conn.close()

The establishment of a fingerprint database by wifi usually requires the average processing of a point of repeatedly collected data. At this time, it is more convenient to use the database command:

insert into wifidb0(BSSID,Strength,Location_id) select BSSID,AVG(Strength),i from wifidb group by BSSID
#Write the average signal strength of the wifi same ap in the data table to the data table wifidb0


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324650479&siteId=291194637