Click here to Skip to main content
15,918,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to store retrieved data in a database of my choice in my laptop using Python?

Is there any setting and connection?


I had done:

pip install mysql.connector


following: Python MySQL - CodersLegacy[^]

After I had tried:
import mysql.connector
 
check = mysql.connector.connect(
    host= 'localhost',
    user = 'root',
    password = '12345678')
 
print(check)



It give me error:

---------------------------------------------------------------------------
InterfaceError                            Traceback (most recent call last)
<ipython-input-4-e72118a94c51> in <module>()
      3 check = mysql.connector.connect(
      4     host= 'localhost',
----> 5     user = 'root')
      6 
      7 

C:\Users\id\AppData\Local\Continuum\anaconda2\lib\site-packages\mysql\connector\__init__.pyc in connect(*args, **kwargs)
    177         return CMySQLConnection(*args, **kwargs)
    178     else:
--> 179         return MySQLConnection(*args, **kwargs)
    180 Connect = connect  # pylint: disable=C0103
    181 

C:\Users\id\AppData\Local\Continuum\anaconda2\lib\site-packages\mysql\connector\connection.pyc in __init__(self, *args, **kwargs)
     93 
     94         if len(kwargs) > 0:
---> 95             self.connect(**kwargs)
     96 
     97     def _do_handshake(self):

C:\Users\id\AppData\Local\Continuum\anaconda2\lib\site-packages\mysql\connector\abstracts.pyc in connect(self, **kwargs)
    714 
    715         self.disconnect()
--> 716         self._open_connection()
    717         self._post_connection()
    718 

C:\Users\id\AppData\Local\Continuum\


What I have tried:

import mysql.connector
 
check = mysql.connector.connect(
    host= 'localhost',
    user = 'root',
    password = '12345678')
 
print(check)
Posted
Updated 18-Jul-20 23:05pm
v3
Comments
Richard MacCutchan 18-Jul-20 7:03am    
Is the MySQL database setup correctly?
Member 14891535 18-Jul-20 7:58am    
I just found this. https://docs.oracle.com/javacomponents/advanced-management-console-2/install-guide/mysql-database-installation-and-configuration-advanced-management-console.htm#JSAMI-GUID-00D8401C-C5EF-4F7C-B211-8B268BA0DB91

Before that, can I know can I setup MySQL database in my personal laptop?
Richard MacCutchan 18-Jul-20 8:37am    
Yes, you must install MySQL before you can use it.
[no name] 18-Jul-20 9:49am    
Yeah, sorry about that. I forgot to mention this in your previous question. You need to setup MySQL first. There are multiple ways of getting a database, but I think directly installing mysql is the easiest.

1 solution

A much simpler way to do this would be to use sqlite3 which is all built in to Python: sqlite3 — DB-API 2.0 interface for SQLite databases — Python 3.7.8 documentation[^].
 
Share this answer
 
Comments
Member 14891535 19-Jul-20 8:03am    
Thanks. I had a look at the documentation provided, I presume is not related to MySQL


import sqlite3
conn = sqlite3.connect('example.db')

c = conn.cursor()

# Create table
c.execute('''CREATE TABLE stocks
(date text, trans text, symbol text, qty real, price real)''')

# Insert a row of data
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

# Save (commit) the changes
conn.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()

It have error:






---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
<ipython-input-12-350b4286edc5> in <module>()
----> 1 c = conn.cursor()
2
3 # Create table
4 c.execute('''CREATE TABLE stocks
5 (date text, trans text, symbol text, qty real, price real)''')

ProgrammingError: Cannot operate on a closed database.
Richard MacCutchan 19-Jul-20 8:25am    
I have no idea what you are doing, but I just copied that exact code to my system and it worked fine.
Member 14891535 19-Jul-20 8:44am    
Can I know are you using ipython notebook to run your code?

Is it the exact code as I paste above?
Richard MacCutchan 19-Jul-20 8:54am    
Yes it is the exact code that you posted. The error message clearly says that the database is not open, so either the open failed somehow, or there is a conn.close() that is not shown.
Member 14891535 19-Jul-20 9:05am    
Thanks. Do you have any idea how to check my connection is working? I presume is not related to MySQL

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900