从Python中重命名SQL中特定表中的列名

  • 本文关键字:Python 重命名 SQL python sql
  • 更新时间 :
  • 英文 :


我正在尝试从Python重命名SQL-Server中的列名。

代码
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)
  • my table is(test_table)
  • 我的列名是(Name)

问题它不工作。它总是到达except块。

有什么问题吗?

我正在使用SQL,这里是错误时,我删除try块:

mysql.connector.errors。ProgrammingError: 1064(42000):你的SQL语法有一个错误;检查与MariaDB服务器版本对应的手册,以便在'EXEC SP_RENAME '测试附近使用正确的语法。Name','First_Name','COLUMN' =

你可以这样使用:

# 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() 

最新更新