gpt4 book ai didi

mysql - 如何使用 ironpython 导入 mysql 数据

转载 作者:行者123 更新时间:2023-11-28 23:33:00 33 4
gpt4 key购买 nike

我正在就如何通过 ironpython 脚本将 sql 数据导入数据库寻求帮助。

目前我的脚本如下,先运行这个脚本:

import clr;
import System;

clr.AddReference("TCAdmin.SDK");
from TCAdmin.SDK.Misc import Random;
from System import String;

ThisService.Variables["MySQLUser"] = String.Format("exile_{0}", ThisService.ServiceId);
ThisService.Variables["MySQLPassword"] = Random.RandomString(10,True,True);
ThisService.Save();

然后这个脚本在之后运行:

import clr;
import System;

clr.AddReference("TCAdmin.DatabaseProviders.MySql");
clr.AddReference("TCAdmin.SDK");
from TCAdmin.DatabaseProviders.MySql import MySqlManager;
from System import String;

mysql_server="localhost";
mysql_root="root";
mysql_password="Password";

with MySqlManager() as mysql:
escapeduser=mysql.PrepareSqlValue(ThisService.Variables["MySQLUser"]);
escapedpass=mysql.PrepareSqlValue(ThisService.Variables["MySQLPassword"]);
mysql.Connect(String.Format("Data Source={0};User Id={1};Password={2};Pooling=False;", mysql_server, mysql_root, mysql_password));

mysql.ExecuteNonQuery(String.Format("DROP DATABASE IF EXISTS {0};", escapeduser));
if mysql.Execute(String.Format("SELECT COUNT(*) as count FROM mysql.user WHERE user='{0}' AND host='localhost';", escapeduser)).Rows[0].Item[0] == 1 :
mysql.ExecuteNonQuery(String.Format("DROP USER {0}@localhost;", escapeduser));

mysql.ExecuteNonQuery(String.Format("CREATE DATABASE {0};", escapeduser));
mysql.ExecuteNonQuery(String.Format("GRANT ALL PRIVILEGES ON {0}.* TO '{0}'@'localhost' IDENTIFIED BY '{1}';", escapeduser, escapedpass));

mysql.ExecuteNonQuery(String.Format("USE '{0}';", escapeduser));
mysql.ExecuteNonQuery(String.Format("""CREATE TABLE IF NOT EXISTS `account` (
`uid` varchar(32) NOT NULL,
`clan_id` int(11) UNSIGNED DEFAULT NULL,
`name` varchar(64) NOT NULL,
`money` double NOT NULL DEFAULT '0',
`score` int(11) NOT NULL DEFAULT '0',
`kills` int(11) UNSIGNED NOT NULL DEFAULT '0',
`deaths` int(11) UNSIGNED NOT NULL DEFAULT '0',
`first_connect_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_connect_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_disconnect_at` datetime DEFAULT NULL,
`total_connections` int(11) UNSIGNED NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;""", escapeduser));

当我运行脚本时出现错误:

您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“exile_2”附近使用的正确语法

TCAdmin.SDK.Database.DatabaseException:您的 SQL 语法有误;检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“exile_2”附近使用的正确语法 ---> 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的“exile_2”附近使用的正确语法 --- 内部异常堆栈跟踪结束 --- 在 Microsoft.Scripting.Interpreter.ThrowInstruction.Run(InterpretedFrame frame ) 在 Microsoft.Scripting.Interpreter.Interpreter.RunInstructions(InterpretedFrame 框架) 在 Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame 框架) 在 Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1) 在 IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx) 在 IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope 范围) 在 Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope 范围) 在 TCAdmin.SDK.Scripting.Engines。 TCAdmin.GameHosting.SDK.Objects.GameScript.ExecuteEventScripts(ScriptEngineManager scriptEngineManager、ServiceEvent eventScript、ObjectList 脚本)在 TCAdmin.GameHost 的 TCAdmin.SDK.Scripting.ScriptEngineManager.Execute() ing.Automation.AutomationProcesses.Ձ() 在 TCAdmin.GameHosting.Automation.AutomationProcesses.Start() 在 TCAdmin.TaskScheduler.ModuleApi.StepBase.Start(对象参数)

最佳答案

所以我能够通过使用以下脚本导入实际的 SQL 文件来解决这个问题。

import clr;
import System;

clr.AddReference("TCAdmin.DatabaseProviders.MySql");
clr.AddReference("TCAdmin.SDK");
from TCAdmin.DatabaseProviders.MySql import MySqlManager;
from System import String;
from System.IO import File, Path;

mysql_server="localhost";
mysql_root="root";
mysql_password="Password";

if not ThisService.Variables.HasValue("MySQLUser") :
Script.Exit();


backupfile=Path.Combine(ThisService.RootDirectory, "dbname.sql");

with MySqlManager() as mysql:
escapeduser=mysql.PrepareSqlValue(ThisService.Variables["MySQLUser"]);
escapedpass=mysql.PrepareSqlValue(ThisService.Variables["MySQLPassword"]);
mysql.Connect(String.Format("Data Source={0};User Id={1};Password={2};Pooling=False;", mysql_server, mysql_root, mysql_password));

mysql.ExecuteNonQuery(String.Format("DROP DATABASE IF EXISTS {0};", escapeduser));
if mysql.Execute(String.Format("SELECT * FROM mysql.user WHERE user='{0}' AND host='localhost';", escapeduser)).Rows.Count == 1 :
mysql.ExecuteNonQuery(String.Format("DROP USER {0}@localhost;", escapeduser));

mysql.ExecuteNonQuery(String.Format("CREATE DATABASE {0};", escapeduser));
mysql.ExecuteNonQuery(String.Format("GRANT ALL PRIVILEGES ON {0}.* TO '{0}'@'localhost' IDENTIFIED BY '{1}';", escapeduser, escapedpass));

if File.Exists(backupfile) :
with MySqlManager() as mysql2:
mysql2.Connect(String.Format("Data Source={0};Database={1};User Id= {1};Password={2};Pooling=False;", mysql_server, ThisService.Variables["MySQLUser"], ThisService.Variables["MySQLPassword"]));
mysql2.ImportDatabase(backupfile);
File.Delete(backupfile);

关于mysql - 如何使用 ironpython 导入 mysql 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36806224/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com