Reputation:
I'm trying to rename a column name in SQL-Server from Python.
def ModifeingColumnName():
try:
myconn = mysql.connector.connect(host ="localhost" , user = "root" , password = "", database = "test")
mycurs = myconn.cursor()
mycurs.execute('''EXEC SP_RENAME 'test_table.Name','First_Name','COLUMN' ''')
myconn.commit()
print('changed successfully!')
except:
print('failed!')
(test)
(test_table)
(Name)
It doesn't work. It always reached the except block.
What's the problem?
I'm using SQL and here is the error when i remove the try block :
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'EXEC SP_RENAME 'test.Name','First_Name','COLUMN'' at line
Upvotes: 0
Views: 725
Reputation: 480
you can use it like this:
# Establish connection to MySQL database
import mysql.connector
db = mysql.connector.connect(
host="localhost", # your host
user="root", # your username
password="root123", # your password
database = "meow" # name of the database
)
mycursor = db.cursor() # cursor() method is used to create a cursor object
query = "ALTER TABLE persons RENAME COLUMN PersonID TO Emp_Id;" # query to rename column
mycursor.execute(query) # execute the query
# close the Connection
db.close()
Upvotes: 2