Posts: 23
Threads: 15
Joined: May 2019
May-08-2019, 08:57 AM
(This post was last modified: May-08-2019, 08:59 AM by buran.)
I am using mysql with python 3.7.2 to get connected my database.
on my windows 2008r2 ,I have installed first sql server 2012 and later also mysql.
I want to know what would be my mysql instance name as localhost would be sql server 2012 and when I am trying to connect it , it gives me access denied error.code is as follows:-
import pymysql
conn=pymysql.connect('localhost','root@localhost','mcse@123','mysqldb')
if not conn!=None:
print("Connection Succeeded")
cmd=conn.cursor()
cmd.execute('create table emp(empid int,empname char(10))')
conn.close()
cmd=conn.cursor()
cmd.execute("insert into emp values(111,'asha')")
conn.commit()
conn.close()
cmd.cursor()
cmd.execute('select * from emp')
result=cmd.fetchall()
for i in result:
print(i)
conn.close() Thanx in adavnce
Posts: 8,154
Threads: 160
Joined: Sep 2016
try
conn=pymysql.connect('localhost','root','mcse@123','mysqldb')
Posts: 23
Threads: 15
Joined: May 2019
(May-08-2019, 08:59 AM)buran Wrote: try
conn=pymysql.connect('localhost','root','mcse@123','mysqldb')
ok sir,when I tried the above mention code..there is no error or result..output is blank.
Posts: 8,154
Threads: 160
Joined: Sep 2016
check this line
if not conn!=None: You want
if conn: or
if conn is not None:
And as a matter of fact - you even don't need it (i.e. to check the conn object) - you will either get connected successfully or you will get exception :-)
Posts: 37
Threads: 13
Joined: Apr 2018
If you are connecting to SQL Server then you have to use pyodbc
import pyodbc
server = 'server_name'
db = 'database_name'
UID = 'user_id'
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db + '; UID = ' + UID + '; PWD = ' + UID + 'Trusted_Connection=yes') or simply
import pyodbc
conn = pyodbc.connect('Trusted_Connection=yes', driver = '{ODBC Driver 13 for SQL Server}',server = 'ServerName', database = 'DBname') Localhost is generic hostname-----> go to run---> cmd ---> hostname ... it will display
Since you are using mysql, it should be taking its own localhost name
If you using sql server, it should be taking its own localhost name as it fetches string info from drivers.
Posts: 8,154
Threads: 160
Joined: Sep 2016
they have both installed, but trying to connect to mysql, not mssql
Posts: 23
Threads: 15
Joined: May 2019
(May-08-2019, 10:05 AM)buran Wrote: check this line
if not conn!=None: You want
if conn: or
if conn is not None: And as a matter of fact - you even don't need it (i.e. to check the conn object) - you will either get connected successfully or you will get exception :-)
It does not even go upto this line it stops at very first line of connection...is it becos there is also sql server 2012 installed on same machine which also takes first instance as localhost???not getting it...
Posts: 8,154
Threads: 160
Joined: Sep 2016
May-09-2019, 07:42 AM
(This post was last modified: May-09-2019, 07:43 AM by buran.)
(May-09-2019, 07:32 AM)srm Wrote: It does not even go upto this line it stops at very first line of connection...is it becos there is also sql server 2012 installed on same machine which also takes first instance as localhost???not getting it...
(May-08-2019, 09:59 AM)srm Wrote: ok sir,when I tried the above mention code..there is no error or result..output is blank.
so, which of your two statements is correct?
both your databases are on localhost, just listen on different port
import pymysql
conn=pymysql.connect('localhost','root','mcse@123','mysqldb')
print("Connection Succeeded")
cmd=conn.cursor()
cmd.execute('create table emp(empid int,empname char(10))')
cmd.execute("insert into emp values(111,'asha')")
cmd.commit()
cmd.execute('select * from emp')
result=cmd.fetchall()
for row in result:
print(row)
conn.close()
Posts: 23
Threads: 15
Joined: May 2019
(May-08-2019, 10:05 AM)buran Wrote: check this line
if not conn!=None: You want
if conn: or
if conn is not None: And as a matter of fact - you even don't need it (i.e. to check the conn object) - you will either get connected successfully or you will get exception :-)
ya I got it ,it was some internal network error with mysql and just reinstalled as it was test machine and I corrected this line of code in python and is working fine.Thanks
|