2

I'm somewhat new to C#, and completely new to SQLite. SQLite is probably way overkill for this project but I wanted to learn it so I'm using it anyway. I'm making a discord bot. The problem is that whenever I do my only command it terminates. I've managed to find the source of the problem and its a command in SQLite to insert data into a row.

m_dbConnection.Open();
string sql = $"INSERT INTO `Cases` ( plaintiffid, accusedid, channelid ) values ( {plaintiff.Id.ToString()}, {accused.Id.ToString()}, {caseChannel.Id.ToString()} )";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
m_dbConnection.Close();

Everytime I start the program it also runs this:

public static SQLiteConnection m_dbConnection { get; private set; }
public static string CasePath { get; } = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"/DiscourtBot/Cases.sqlite";
public static void Start()
{
    if (!File.Exists("Cases.sqlite"))
    {
        SQLiteConnection.CreateFile("Cases.sqlite");
        m_dbConnection = new SQLiteConnection("Data Source=Cases.sqlite;Version=3;");
        m_dbConnection.Open();
        string sql = "CREATE TABLE `Cases` ( plaintiffid INTEGER, accusedid INTEGER, channelid INTEGER UNIQUE )";
        SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
        command.ExecuteNonQuery();
        m_dbConnection.Close();
    }
    else m_dbConnection = new SQLiteConnection("Data Source=Cases.sqlite;Version=3;");
}

No exceptions are thrown.

I've tested a bit of the code in another program and this code also causes it to terminate.

SQLiteConnection.CreateFile("Cases.sqlite");
m_dbConnection = new SQLiteConnection("Data Source=Cases.sqlite;Version=3;");
m_dbConnection.Open();
string sql = "CREATE TABLE `Cases` ( plaintiffid BIGINT, accusedid BIGINT, channelid BIGINT )";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
m_dbConnection.Close();

m_dbConnection.Open();
string sqll = $"INSERT INTO `Cases` ( plaintiffid, accusedid, channelid ) values ( 5, 5, 5 )";
SQLiteCommand commandd = new SQLiteCommand(sqll, m_dbConnection);
commandd.ExecuteNonQuery();
m_dbConnection.Close();

Discord ID's usually look something like this: 394343217715216384 I'd appreciate it if anyone could help me.

7
  • and what exception you get? Commented Feb 10, 2018 at 21:52
  • 1
    "it crashes" is not a good problem description. Did you get an error/exception? What's the message? Commented Feb 10, 2018 at 21:52
  • terminates would be a better way to describe it. What datatype could handle that number? Commented Feb 10, 2018 at 22:02
  • I have tested your code and there is no error. Did you check if the record has been inserted? Commented Feb 10, 2018 at 22:05
  • 1
    Thats not the correct way to create SQL. Especially with SQLite you want to use SQL PArameters. Note that you define the table with INTEGER, then pass foo.bar.Id.ToString() Commented Feb 10, 2018 at 23:04

1 Answer 1

1

After a bit of messing around I found out that an API I was using was interfering with a constructor. I solved the problem by putting the INSERT INTO code in the same place where I called the constructor.

I'm still not completely sure what was going on but it's fixed now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.