English 中文(简体)
无法执行手稿 - 释放或引渡
原标题:Unable to execute mysql script - CREATE FUNCTION or CREATE TRIGGER via Python

我确实对我前面写的我的手稿感到麻烦,试图用我的手法阅读,也执行。 在这里,我简单的我的手稿被删除。

DROP FUNCTION IF EXISTS `employee_db`.`func_create_token`;

CREATE FUNCTION `employee_db`.`func_create_token`() RETURNS VARCHAR(100) DETERMINISTIC
BEGIN
    DECLARE `chars` VARCHAR(26);
    DECLARE `charLen` INT;
    DECLARE `randomPassword` VARCHAR(100);
    
    SET `chars` =  ABCDEFGHIJKLMNOPQRSTUVWXYZ ;
    SET `charLen` = LENGTH(`chars`);
    SET `randomPassword` =   ;

    WHILE LENGTH(`randomPassword`) < 12 DO
        SET `randomPassword` = CONCAT(`randomPassword`, SUBSTRING(`chars`, CEILING(RAND() * `charLen`), 1));
    END WHILE;

    RETURN `randomPassword`;
END;

我能够直接执行这一职能。 迄今没有任何问题。 但是,试图读一下金字的q子,然后加以执行,总是会导致错误,我有些how可以解决。 下面请看一看 p的功能:

# Function to execute sql file
def execute_sql_script(path_to_sql: os.PathLike[str], title: str) -> None:
    
    # Read sql file from folder `sql`
    try:
        with open(path_to_sql,  r ) as f:
            queries : list = f.read().split( ; )
            queries : list = [q.strip() for q in queries if q.strip()]
        f.close()
    except FileNotFoundError as f:
        print(color.BOLD + color.RED + f File couldn t be read. Please check error: {e}  + color.END)
        
    # Execute sql script with progress bar
    try:
        with alive_bar(len(queries), dual_line = True, title = title, force_tty = True) as bar:
            for _, query in enumerate(queries):
                sleep(.05)
                cursor.execute(query)
                bar()
        cursor.commit()
    except Error as e:
        print(color.BOLD + color.RED + f Error in executing sql script. Check error: {e}  + color.END)

错误信息非常有用:

Create Function - Employee |███▋⚠︎                                   | (!) 1/11 [
[1m[91mError in executing sql script. Check error: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near    at line 3[0m

奇怪的是,每处表格的所有其他文字的执行,都是热情的,没有任何例外。

Create Function - Employee |████████████████████████████████████████| 27/27 [100
问题回答

您读到文字,将这种投入分成关于<代码>的单项内容表; 特性,并执行其中每一项发言。

但是,MySQL的常规体内有<条码>;。 如果你把关于<代码>的意见分开; 性质,请:

CREATE FUNCTION `employee_db`.`func_create_token`() RETURNS VARCHAR(100) DETERMINISTIC
BEGIN
    DECLARE `chars` VARCHAR(26);

这并不是一份完整的《任择议定书》声明。 它过早地终止了第一个<代码>; 性质,因为你告诉它如何分拨投入。 该名教授希望<条码>BEGIN有<>END,但该代码为t。

This is why simply splitting on ; is not sufficient. You would need to keep track of any BEGIN...END blocks, including nested blocks.

You would also need to parse the input to make sure you aren t finding a ; character that s part of a string literal, or inside a comment, or even part of an SQL identifier.

你们基本上需要一个完整的编织品,而不仅仅是f.read(.split(;)。

A second alternative is to do what the MySQL client does when it reads an SQL script: you d need to support the DELIMITER command, and use delimiters in your SQL script that are not ;, and not any other character found in the body of the stored routine. You d need to parse the input line by line, until you found the current delimiter (since it won t be ;).

第三个选择是忘记关于“经营”笔记的书写代码,而只是为了管理<密码>-mysql的客户,而后者已经是必要的。

It s up to you. You can spend hours or days writing a parser that does this in Python. Or you could run the mysql client and be done with your task in five minutes.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签