1

i am seeking assistance on a way I can import sql data into a database via ironpython script.

currently my script is as follows, first this script runs:

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();

then this script runs after:

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));

when I run the script I get the error:

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 ''exile_2'' at line 1

TCAdmin.SDK.Database.DatabaseException: 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 ''exile_2'' at line 1 ---> 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 ''exile_2'' at line 1 --- End of inner exception stack trace --- at Microsoft.Scripting.Interpreter.ThrowInstruction.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.RunInstructions(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame) at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1) at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx) at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope) at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope) at TCAdmin.SDK.Scripting.Engines.IronPythonEngine.Execute(Credentials credentials) at TCAdmin.SDK.Scripting.ScriptEngineManager.Execute() at TCAdmin.GameHosting.SDK.Objects.GameScript.ExecuteEventScripts(ScriptEngineManager scriptEngineManager, ServiceEvent eventScript, ObjectList scripts) at TCAdmin.GameHosting.Automation.AutomationProcesses.Ձ() at TCAdmin.GameHosting.Automation.AutomationProcesses.Start() at TCAdmin.TaskScheduler.ModuleApi.StepBase.Start(Object arguments)

1 Answers1

1

So i was able to solve this by importing the actual SQL file with the following script.

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);