2016-04-23 17 views
1

ich suche unterstützung auf eine weise ich kann sql daten in eine datenbank über ironpython script importieren.wie importiere ich mysql daten mit ironpython

zur Zeit mein Skript wird wie folgt zunächst das Skript ausgeführt wird:

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

dann dieses Skript ausgeführt nach:

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

, wenn ich das Skript ausführen ich den Fehler:

Sie haben einen Fehler in Ihrer SQL-Syntax. Überprüfen Sie das Handbuch, das Ihrer MySQL-Server-Version für die richtige Syntax in der Nähe von '' exile_2 '' in Zeile 1 entspricht

TCAdmin.SDK.Database.DatabaseException: Sie haben einen Fehler in Ihrer SQL-Syntax; Überprüfen Sie das Handbuch, das Ihrer MySQL-Serverversion entspricht, nach der richtigen Syntax für die Verwendung in der Nähe von '' exile_2 '' in Zeile 1 ---> Sie haben einen Fehler in Ihrer SQL-Syntax; Überprüfen Sie das Handbuch, das Ihrer MySQL-Server-Version für die richtige Syntax in der Nähe von '' exile_2 '' in Zeile 1 entspricht --- Ende der inneren Ausnahme Stack-Trace --- bei Microsoft.Scripting.Interpreter.ThrowInstruction.Run (InterpretedFrame-Rahmen) bei Microsoft.Scripting.Interpreter.Interpreter.RunInstructions (InterpretedFrame frame) bei Microsoft.Scripting.Interpreter.Interpreter.Run (InterpretedFrame frame) bei Microsoft.Scripting.Interpreter.LightLambda.Run2 [T0, T1, TRet] (T0 arg0, T1 arg1) bei IronPython.Compiler.PythonScriptCode.RunWorker (CodeContext ctx) unter IronPython.Compiler.RuntimeScriptCode.InvokeTarget (Scope-Bereich) unter Microsoft.Scripting.Hosting.ScriptSource.Execute (ScriptScope-Bereich) unter TCAdmin.SDK.Scripting.Engines. IronPythonEngine.Execute (Berechtigungsnachweise) unter TCAdmin.SDK.Scripting.ScriptEngineManager.Execute() bei TCAdmin.GameHosting.SDK.Objects.GameScript.ExecuteEventScripts (ScriptEngineManager scriptEngineManager, ServiceEvent eventScript, ObjectList-Skripts) bei TCAdmin.GameHost ing.Automation.AutomationProcesses. Ձ() bei TCAdmin.GameHosting.Automation.AutomationProcesses.Start() bei TCAdmin.TaskScheduler.ModuleApi.StepBase.Start (Object Argumente)

Antwort

1

So war ich in der Lage, dies zu lösen, indem die tatsächlichen Import SQL-Datei mit dem folgenden Skript.

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