Jump to content

Python mysql.connector - how am I supposed to use it? (Mac)

Wictorian

I really have no idea what I am supposed to do. I know I have to create a database first to run the code but no one talks about how. I have written the code but can't run it becasue I don'T know how to create the database.

 

Link to comment
Share on other sites

Link to post
Share on other sites

This would be easiest to do within Docker; I would highly suggest looking at creating a Docker container and setting up an SQL server & DB, and running your python program within.

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

What have you tried?

What have you researched?

What code have you written?

 

What is the actual error you are receiving or functionality you are expecting?

 

Have you read the docs for mySQL?

Have you read any tutorials?

Have you watched any videos?

 

1 hour ago, Wictorian said:

no one talks about how.

You and I know this is not true. There are 5 million videos, articles, documents, and tutorials on how to create a mySQL database.

 

As a hint, The first thing that showed up on ddg when I searched for "Create a mysql database on mac" was a step by step guide, with pictures and detailed explanations on how to do what  you are asking.

If your question is answered, mark it so.  | It's probably just coil whine, and it is probably just fine |   LTT Movie Club!

Read the docs. If they don't exist, write them. | Professional Thread Derailer

Desktop: i7-8700K, RTX 2080, 16G 3200Mhz, EndeavourOS(host), win10 (VFIO), Fedora(VFIO)

Server: ryzen 9 5900x, GTX 970, 64G 3200Mhz, Unraid.

 

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Takumidesh said:

What have you tried?

What have you researched?

What code have you written?

 

What is the actual error you are receiving or functionality you are expecting?

 

Have you read the docs for mySQL?

Have you read any tutorials?

Have you watched any videos?

 

You and I know this is not true. There are 5 million videos, articles, documents, and tutorials on how to create a mySQL database.

 

As a hint, The first thing that showed up on ddg when I searched for "Create a mysql database on mac" was a step by step guide, with pictures and detailed explanations on how to do what  you are asking.

Everyone talks about the code but when I run it I get error 61. Which I suppose means that database hasn’t been created. As far as I understand I should create the database outside the code first. And no one talks about that so far.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Wictorian said:

And no one talks about that so far.

Who you referring to? Maybe the exercise presumes you already know mysql.?

 

You can create/manage your databases using the mysql command line. Or install mysql workbench for a gui.

https://www.mysqltutorial.org/mysql-create-database/

 

If you want to create a database using python's mysql connector. This guide explains

https://realpython.com/python-mysql/

Link to comment
Share on other sites

Link to post
Share on other sites

36 minutes ago, C2dan88 said:

Who you referring to? Maybe the exercise presumes you already know mysql.?

 

You can create/manage your databases using the mysql command line. Or install mysql workbench for a gui.

https://www.mysqltutorial.org/mysql-create-database/

 

If you want to create a database using python's mysql connector. This guide explains

https://realpython.com/python-mysql/

I have trouble connecting to mysql server.

Link to comment
Share on other sites

Link to post
Share on other sites

Your MySQL server is listening on port 3306 by default. Connect to it via 

mysql --protocol=TCP -u <username> -p

 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

44 minutes ago, wasab said:

Your MySQL server is listening on port 3306 by default. Connect to it via 

mysql --protocol=TCP -u <username> -p

 

What am I supposed to substitute username with?

I haven’t created users or anything.

And aren’t I supposed to create the server first?

Link to comment
Share on other sites

Link to post
Share on other sites

17 minutes ago, Wictorian said:

I haven’t created users or anything.

Mysql's default username is root. With no password.

 

17 minutes ago, Wictorian said:

And aren’t I supposed to create the server first?

Mysql is the server. Have you actually installed mysql? And is it running?

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Wictorian said:

Everyone talks about the code but when I run it I get error 61. Which I suppose means that database hasn’t been created. As far as I understand I should create the database outside the code first. And no one talks about that so far.

Hey, so for context - here's how it works: 

 

You setup a mysql database or whatever database you're using.  This needs to be set-up as a server, and then you log into it and create a database.  You can create a user to access MySQL on the server you set up. 

 

The MySQL connector allows the Python application to access your MySQL database server and send queries to retrieve or post data.  

 

This makes the beginning of a 3-tier architecture to allow for scalability, it's quite old and probably won't be used outside of development but it's great to learn for sure as it leads onto more complex system decoupling. https://en.wikipedia.org/wiki/Multitier_architecture if you want to learn about it. 

 

Now, if you want to set-up the database - you have a couple of options:

Finally, make sure if you do opt to use the cloud option you secure your database by using strong credentials and setting your security group to only allow connections from your IP address (make sure /32 bit netmask). 

 

If this sounds complex to you, I'd take a course or follow a tutorial on setting up and using MySQL first so you can know what you're doing.   Taking a course on a cloud platform like AWS or GCP will be helpful as well so you can spin up development resources such as databases quickly. 

 

This site looks like a great starting point for the basics: https://www.tutorialspoint.com/mysql/index.htm if this does sound complex. 

 

Link to comment
Share on other sites

Link to post
Share on other sites

16 hours ago, Wictorian said:

Everyone talks about the code but when I run it I get error 61. Which I suppose means that database hasn’t been created. As far as I understand I should create the database outside the code first. And no one talks about that so far.

What code? what is giving that error? did you make the database? what do you mean no one talks about making a database?

 

some helpful tips on how to actually ask a technical question can be found here:

https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/

https://stackoverflow.com/help/how-to-ask

 

 

What about all the other questions I asked? if you elaborate more, demonstrate the research you did, give code examples, etc. people can help you more.

 

Did you read the mysql docs where they tell you explicitly how to setup a mysql database on several operating systems in detail? did you put any effort into actually solving the problem?

 

I, just now, literally typed into google "how to create a mysql database" and the very first result is the official docs detailing exactly how to do it. did you read those?

 

You are saying the mystical 'nobody' is talking about how to do this, yet it is literally the first result that comes up, straight from the horses mouth.

 

once again... did you read any of the official documentation at all?

 

 

If your question is answered, mark it so.  | It's probably just coil whine, and it is probably just fine |   LTT Movie Club!

Read the docs. If they don't exist, write them. | Professional Thread Derailer

Desktop: i7-8700K, RTX 2080, 16G 3200Mhz, EndeavourOS(host), win10 (VFIO), Fedora(VFIO)

Server: ryzen 9 5900x, GTX 970, 64G 3200Mhz, Unraid.

 

Link to comment
Share on other sites

Link to post
Share on other sites

20 hours ago, Takumidesh said:

What code? what is giving that error? did you make the database? what do you mean no one talks about making a database?

 

some helpful tips on how to actually ask a technical question can be found here:

https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/

https://stackoverflow.com/help/how-to-ask

 

 

What about all the other questions I asked? if you elaborate more, demonstrate the research you did, give code examples, etc. people can help you more.

 

Did you read the mysql docs where they tell you explicitly how to setup a mysql database on several operating systems in detail? did you put any effort into actually solving the problem?

 

I, just now, literally typed into google "how to create a mysql database" and the very first result is the official docs detailing exactly how to do it. did you read those?

 

You are saying the mystical 'nobody' is talking about how to do this, yet it is literally the first result that comes up, straight from the horses mouth.

 

once again... did you read any of the official documentation at all?

 

 

Yeah I read the docs and it doesn't talk about creating the server. By database I meant the server. Sorry for confusion.

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Wictorian said:

Yeah I read the docs and it doesn't talk about creating the server. By database I meant the server. Sorry for confusion.

creating a sql server involves just you installing the database and then running it. if you dont like writing sql commands, you can just create some migration code in python to create the schema and tables, maybe also seed them with some initial rows if you want. 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

  • 4 months later...
On 10/21/2022 at 11:53 PM, Wictorian said:

I really have no idea what I am supposed to do. I know I have to create a database first to run the code but no one talks about how. I have written the code but can't run it becasue I don'T know how to create the database.

 

you need a DataBase Management System, once you install and setup one, you can create run normal sql commands and create stuff. you can get started by installing something like PostgreSQL or MySQL

 

you can run this to create your database named "mydatabase"

CREATE DATABASE mydatabase;

 

and this is how you use mysql connector in python to connect to that database

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  database="mydatabase"
)

you can read the docs here: https://dev.mysql.com/doc/connector-python/en/

you can refer this part of the documentation to create a table

 

hey! i know to use a computer

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×