From our previous post, we used python to read data from mysql. In this post we will be using the random
library to write random data into mysql.
We will define our lists with the categorized data, and then using for loop, to write data into our mysql database:
Create The Database:
Using Python to Create the Database:
1 2 3 4 5 6 |
|
Now to list our databases:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Python Code to Write to MySQL
We will create a mysql_write.py
file, with the following contents to define our random data that we will write to our mysql database. The config module can be found from this post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
After running the file: python mysql_write.py
we should have 10 records in our database.
Reading the Data from MySQLL
To verify that the data is in our MySQL Database, lets logon to our mysql database:
1
|
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Next, lets use Python to do the same, create a file mysql_read.py
with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Running the Python file, to read the data:
1 2 3 4 5 6 7 8 9 10 11 12 |
|