Installing mySQL connector to connect python
with php myAdmin in Raspberry Pi .
To run mySQL with python we need to establish connection between mySQL and python in Raspberry pi. We already install mysql with php myadmin .
(if phpmyadmin is not install in your raspberrypi then visit : https://aiworldofjuhi.blogspot.com/2019/02/how-to-install-mysql-on-raspberry-pi-3b.html ).
(if phpmyadmin is not install in your raspberrypi then visit : https://aiworldofjuhi.blogspot.com/2019/02/how-to-install-mysql-on-raspberry-pi-3b.html ).
now, go to command prompt or terminal in raspberry pi and install bellow library.
Step 1 : $sudo apt-get update
Step 2 : $sudo apt-get upgrade
Step 3 : $sudo apt-get -y install python-mysql.connector
After installing python-mysql.connector python is ready to work with mysql. here, An example of inserting data in php myadmin using python-mysql.coonector.
Way: 1
import mysql.connector
mydb=mysql.connector.connect(host='localhost',user='raspberry',passwd='juhi',database='chanchala')
print ("working")
mycursor=mydb.cursor()
#mycursor.execute("CREATE TABLE pixs(line INTEGER(2), xcor INTEGER(5), ycor
INTEGER(5))")
sqlFormula="INSERT INTO pixs(line,xcor,ycor) VALUES(1,2,3)"
#pixs1=[(1,2,3),(5,4,7),(8,9,6)]
mycursor.execute(sqlFormula)
mydb.commit()
Way: 2
import mysql.connector
mydb=mysql.connector.connect(host='localhost',
user='hiren',passwd='hiren',database='chanchala')
print ("working")
mycursor=mydb.cursor()
#mycursor.execute("CREATE TABLE pixs(line INTEGER(2), xcor INTEGER(5), ycor
INTEGER(5))")
sqlFormula="INSERT INTO pixs(line,xcor,ycor) VALUES(%s,%s,%s)"
pixs1=[(1,2,3),(5,4,7),(8,9,5)]
mycursor.executemany(sqlFormula ,pixs1)
mydb.commit()
This picture shows how a table is created and store data one by one through python-mysql.connector.
Next blog start with computer vision system and some real time image processing programming.
Nice work..
ReplyDelete