Mysql/Maridb Python连接器未将数据加载到表中

Mysql/Maridb Python连接器未将数据加载到表中,python,mysql,sql,mariadb,Python,Mysql,Sql,Mariadb,代码检查数据库中是否有同名的表,然后可能会删除它,创建一个新表并将csv文件的数据加载到表中。 执行代码时,似乎一切正常,但在mariadb命令提示符下运行时,创建的表为空,即使在代码中输出表时已填充。默认情况下,mariadb Connector/Python不使用自动提交模式 建立连接时,您需要设置autocommit=True,或者必须使用conn.commit()Thx提交更改。我在文档中完全忽略了这一点。 # Module Imports import mariadb import s

代码检查数据库中是否有同名的表,然后可能会删除它,创建一个新表并将csv文件的数据加载到表中。
执行代码时,似乎一切正常,但在mariadb命令提示符下运行时,创建的表为空,即使在代码中输出表时已填充。

默认情况下,mariadb Connector/Python不使用自动提交模式


建立连接时,您需要设置
autocommit=True
,或者必须使用
conn.commit()

Thx提交更改。我在文档中完全忽略了这一点。
# Module Imports
import mariadb
import sys
import csv
from pathlib import Path

def connect_to_mariaDB(databse, user, passwd):
    # Connect to MariaDB Platform
    try: conn = mariadb.connect( 
        user=user, 
        password=passwd, 
        host="localhost", 
        port=3306, 
        database=databse 
    ) 
    except mariadb.Error as e: 
        print(f"Error connecting to MariaDB Platform: {e}") 
        sys.exit(1) 
    return conn

def check_if_table_exists_and_overwrite(conn, tableName, database, overwrite):
    cur = conn.cursor() 
    cur.execute(f"SELECT table_name FROM information_schema.tables WHERE table_schema = '{database}';") 
    for(table_name) in cur:
        if table_name[0] == tableName:
            if overwrite == "YES":
                print("table exists - DROP TABLE")
                cur.execute(f"DROP TABLE {tableName}")
                return True        
            else:
                return False
    return True

def import_file_into_db_table_(
filename, database, user, passwd, tableName,
create_table_statement = "", overwrite = False):

    conn = connect_to_mariaDB(database, user, passwd)
    cur = conn.cursor()
    if conn != None:
        print(f"Connection successful to database {database}")
        
        if check_if_table_exists_and_overwrite(conn, tableName, database, overwrite):
            cur.execute(create_table_statement)
            print("table is created")
            
            path = f"{Path().absolute()}\\{filename}".replace("\\","/")
            print(path)
            load_data_statement = f"""LOAD DATA INFILE '{path}' 
                                    INTO TABLE {tableName} 
                                    FIELDS TERMINATED BY ';' 
                                    OPTIONALLY ENCLOSED BY '\"' 
                                    LINES TERMINATED BY '\\n' 
                                    IGNORE 1 LINES
                                    """
            print(load_data_statement)
            cur.execute(load_data_statement)
            print("load data into table - successful")
            
        else:
            print("table exists - no permission to overwrite")

    cur.execute("SELECT * FROM student_mat;")
    for da in cur: 
        print(da)
        

# variables
filename = "student-mat.csv"
database = "dbs2021"
tableName = "student_mat"

# load the create_table_statement
create_table_statement = ""
path = f"{Path().absolute()}\\create_table_statement.txt"
with open(path, newline='') as file:
    spamreader = csv.reader(file, delimiter='\n', quotechar='|')

    for row in spamreader:
        create_table_statement += row[0]
        
parameters_length = len(sys.argv)
if parameters_length == 3:
    user, passwd = sys.argv[1], sys.argv[2]
    import_file_into_db_table_(filename, database, user, passwd, tableName, create_table_statement, "YES")
elif parameters_length == 4:
    user, passwd, overwrite = sys.argv[1], sys.argv[2], sys.argv[3]
    import_file_into_db_table_(filename, database, user, passwd, tableName, create_table_statement, overwrite)
else:
    print("wrong parameters\nTry -user -passwd or additional -overwrite")