Connecting to SQL Data Source from Python code

已回答

I've set up a remote MariaDB data source in the database dialog; no trouble here.  It connects, I can execute SQL queries in the console, view schemas, etc.  What I cannot do, however, is figure out how to get the Python code to actually use that data source.  Everything I've searched for via Google returns 'how to set up a data source' or 'how to execute a query in the console'.  I don't have a problem with the console; I have a problem executing a query in the code.  I can execute the Python script just fine on a remote server when I copy it there, but I can't get the code to connect within PyCharm which means debugging via PyCharm won't work, I can't run any test cases, etc.

0

Please go to Settings -> Languages & frameworks -> SQL Resolution scopes -> Set up project mapping to your Data Source.

0
Avatar
Permanently deleted user

I appreciate the response.  Unfortunately, this doesn't help me.  The project mapping has already been selected and that doesn't appear to tell me how to get the code to use the data source.  Is there something more?  Do I need to add a few lines of configuration to my script?  Do I need to make changes to a special file(like .NET's ".config")?  Should my 'mysql.connector.connect()' call specify "myDataSourceMapping" as the connection host?  This is where my confusion comes in... there's nothing that tells me how to actually **use** the data source within the code.

0

You need to specify  the same parameters for DB connection in your script as on your remote server to be able to run script in IDE and get SQL queries work.

Data Source you added to the project is used for auto completion when you write SQL queries inside your code and to run these SQL queries from the console.

0

Could you provide a sample code?

I can connect to the Microsoft SQL database in pycharm. I can use the console from pycharm to query. But I have no idea how to connect to the database, query and read into pandas dataframe using a python file in pycharm

Thanks

0

Typically, to connect to database source from python code, a specific library is needed. There are many libraries for different DB servers. Please google something like "python <database name> connector".

>I can execute the Python script just fine on a remote server when I copy it there, but I can't get the code to connect within PyCharm

If you can't connect to remote database from your local PyCharm instance, consider using PyCharm remote debugger

0

请先登录再写评论。