首页 > 解决方案 > 我的 python 代码在创建模式和更新外键时跳过 SQL 语句,请建议

问题描述

我对 python 还很陌生,我编写了一个 python 代码,它使用 mysql.connector 包执行 Mysql 脚本以“创建表模式并添加外键”。我正在为多个表运行此脚本,一次使用 3 个 sql 语句。该代码在本地计算机上运行时执行良好,但在 ISPAN 中运行时缺少创建一些表和更新外键,尽管执行成功。

以下是我创建的示例脚本:

use prime; create table Address(
Address_Uniq_Identifier varchar(25) Not Null
,Address_Locat_Name varchar(50) Not Null
,Address_Line_1 varchar(100) Not Null
,Address_Line_2 varchar(100) Default Null
,Source_Reference varchar(25) Default Null
,Primary Key (Address_Uniq_Identifier));

use prime; create table People(
People_Uniq_Identifier varchar(25) Not Null
,Legal_Entity int(1) Not Null
,Title varchar(20) Default Null
,Last_Name varchar(30) Not Null
,Source_Reference varchar(25) Default Null
,Primary Key (People_Uniq_Identifier));

use prime; create table Account(
Account_Uniq_Identifier varchar(25) Not Null
,Customer_Uniqu_Identifier varchar(25) Not Null
,Primary_Acco_Uni_Identifier varchar(25) Not Null
,Financial_Statu_Code varchar(4) Default Null
,Authorization_Statu_Code varchar(4) Default Null
,Source_Reference varchar(25) Default Null
,Primary Key (Account_Uniq_Identifier));

use prime; create table Customer(
Customer_Uniq_Identifier varchar(25) Not Null
,People_Uniqu_Identifier varchar(25) Not Null
,Customer_Type int(5) Not Null
,Source_Reference varchar(25) Default Null
,Primary Key (Customer_Uniq_Identifier));

use prime; Alter table Customer
 ADD FOREIGN KEY (People_Uniqu_Identifier)  REFERENCES People (People_Uniq_Identifier);

以下是代码片段

    //Reading file       
    //Making SQL connectivity      
    mydb = mysql.connector.connect(
        host="**7.*.*.1",
        user="r**t",
        password="N***a@*0*1",
        database="prime"
        )   
    mycursor = mydb.cursor()   
    //Creating script   
    //Executing script 3 at a time using the following code in loop   
    mycursor.execute(record_exe)  //*executing the first 3 sql stmt   
    mycursor.fetchall()   
    mycursor.close()              //* closing sql cursor   
    mydb.close()                  //* closing database connectivity   
    record_exe = ""   //* initialising the sql statement with spaces   
    count_semi =0    
    mydb = mysql.connector.connect(
    host="**7.*.*.1",              //* creating the connectivity again
    user="r**t",
    password="N***a@*0*1",
    database="prime"
    )    
    mycursor = mydb.cursor()      //* creating a new cursor    
    time.sleep(5)                 //* added sleep time to counter for lag on ISPAN    

当我在 ISPAN 上运行这段代码时,问题就出现了,虽然它执行得很好,但有些表丢失了,外键更新没有完成。请帮助建议一种方法来检查它为什么会跳过一些 sql 命令

标签: pythonmysql

解决方案


推荐阅读