SQLite没有';t创建数据库表,导致irc bot崩溃

本文关键字:导致 irc 崩溃 bot 数据库 创建 没有 SQLite | 更新日期: 2023-09-27 18:20:45

因此,当我连接或尝试时,它会在Database.cs中运行此代码。此外,我正在使用SmartIRC4Net处理IRC现在我知道这是错误,因为Database.cs中的Init()甚至没有运行!如果是,它就不会创建带有表的"trubot.sqlite"文件。

我不知道它为什么这么做,但确实如此。

这是Database.cs代码:

    public void Init(){
        try {
            if (File.Exists("trubot.sqlite")) {
                dbf = new SQLiteConnection("Data Source=trubot.sqlite;Version=3");
                dbf.Open();
                String db;
                db = "CREATE TABLE IF NOT EXISTS '"+chan+"' (id INTEGER PRIMARY KEY, user TEXT, currency INTEGER DEFAULT 0, subscriber INTEGER DEFAULT 0, battletag TEXT DEFAULT null, uLevel INTEGER DEFAULT 0, mod INTEGER DEFAULT 0, rlvl INTEGER DEFAULT 0);";
                using (query = new SQLiteCommand(db, dbf)){
                    query.ExecuteNonQuery();
                }
            } else {
                 SQLiteConnection.CreateFile("trubot.sqlite");
                 dbf = new SQLiteConnection("Data Source=trubot.sqlite;Version=3");
                 dbf.Open();
                 String db;
                 db = "CREATE TABLE IF NOT EXISTS '"+chan+"' (id INTEGER PRIMARY KEY, user TEXT, currency INTEGER DEFAULT 0, subscriber INTEGER DEFAULT 0, battletag TEXT DEFAULT null, uLevel INTEGER DEFAULT 0, mod INTEGER DEFAULT 0, rlvl INTEGER DEFAULT 0);";
                 using (query = new SQLiteCommand(db, dbf)){
                    query.ExecuteNonQuery();
                }
            }
        } catch (Exception s) {
            Console.WriteLine("[ERROR] Error in code. " + s.Message);
        }
    }
    public void addUser(String user) {
       // add new user
        try {
            if (!usrExist(user)) {
               String db = "INSERT INTO '"+chan+"' (user) VALUES ('"+user+"');";
               using (query = new SQLiteCommand(db,dbf)) {
                    query.ExecuteNonQuery();
               }
            }
       } catch (Exception err) {
               Console.WriteLine("addUser is causing an error: " + err.Message);
           }
    }

这是它崩溃的另一个原因(在Program.cs中)

public static void OnJoined(object sender, JoinEventArgs e) {
    try {
        var conf = new Config();
        var db = new Database();
        Console.WriteLine("[SELF] ["+conf.Channel+"] > *** "+e.Data.Nick+" has joined the channel!");
        if (!db.usrExist(e.Data.Nick)) {
            try {
                db.addUser(e.Data.Nick);
            } catch (Exception er1) {
               string lnNum = er1.StackTrace.Substring(er1.StackTrace.Length - 7, 7);
               Console.WriteLine("Error: -- Trubot Error "+ er1.Message + " " + er1.Data.ToString()
                          + " " + er1.InnerException.Message.ToString()
                          + " " + er1.TargetSite.ToString() + " Ln: " + lnNum);
               Console.ReadKey();
            }
        }
    } catch (Exception er1) {
         string lnNum = er1.StackTrace.Substring(er1.StackTrace.Length - 7, 7);
         Console.WriteLine("Error: -- Trubot Error "+ er1.Message + " " + er1.Data.ToString()
               + " " + er1.InnerException.Message.ToString()
               + " " + er1.TargetSite.ToString() + " Ln: " + lnNum);
         Console.ReadKey();
    }
}

附带说明:我会使用MySQL,但我需要这个应用程序尽可能便携,并在尽可能多的操作系统上运行。我宁愿使用SQLite而不是MSSQL或MySQL。

SQLite没有';t创建数据库表,导致irc bot崩溃

我修复了它。问题是当我将通道作为表名分配给SQL时,我需要从中删除"#"

public Database() {
    var conf = new Config();
    chan = conf.Channel.Replace("#","");
    Init();
}