我正在寻求帮助,我可以通过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框架) )在Microsoft.Scripting.Interpreter.LunLnsda.Run2 [T0,T1,TRet]的Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame框架)中的Microsoft.Scripting.Interpreter.Interpreter.RunInstructions(InterpretedFrame框架)(T0 arg0, T1 arg1)位于TCAdmin.SDK.Scripting.Engines的Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope范围)的IronPython.Compiler.RuntimeScriptCode.InvokeTarget(范围范围)中的IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)。 TCAdmin.GameHost上的TCAdmin.GameHosting.SDK.Objects.GameScript.ExecuteEventScripts(ScriptEngineManager scriptEngineManager,ServiceEvent eventScript,ObjectList脚本)中的TCAdmin.SDK.Scripting.ScriptEngineManager.Execute()上的IronPythonEngine.Execute(凭证凭证) ing.AutomationProcesses。Ձ()at TCAdmin.GameHosting.Automation.AutomationProcesses.Start()at TCAdmin.TaskScheduler.ModuleApi.StepBase.Start(Object arguments)
答案 0 :(得分:1)
所以我能够通过使用以下脚本导入实际的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);