Of course, when we’re doing our data analysis reading from and writing to a database is super important. In this article, I am going to cover how we do that with MySQL. We’ll start at the most simple level and progress up to more complicated methods.
In this first example, we make the database connection using our connection string. Note that I don’t define the host. If you’re using a localhost, you can just leave that bit out.
In this example, we hard code the values that we want to insert into the database table.
In this second example, I’ve defined a couple of variables, which I then pass into the SQL statement. This is a big step forward on the example above.
Here, you can see how I’ve implemented this solution in the Twitter script which I have been discussing in recent articles.
The final thing is, saving data from a dataframe to MySQL, which is really very handy. You can see in the below, we create the DB connection and then simply use the ToSQL function on the dataframe. IMPORTANT: The names and data types in your database table must exactly match those of your QFrame.