0

I'm trying to add a record to my table using string variables for VarChar columns. I want to insert the values I give it under the next incremented ID. However, I keep getting the SQL error that "no such column exists" when I try and run the code. There is no elaboration on where the error occurs, so I'm sorta stuck. So how can I insert records into a table?

Here's my code

using UnityEngine;
using Mono.Data.Sqlite;
using System.Data;
using UnityEngine.UI;
using System.Collections;

public class AddQuery : MonoBehaviour {

    public Text cipher;
    public Text initialMessage;
    public Text encryptedMessage;

    private string _constr = "URI=file:previousMessages.db";
    private IDbConnection _dbc;
    private IDbCommand _dbcm;
    private IDataReader _dbr;


public void AddSQL() {
    string _cipher = cipher.text;
    string _initialMessage = initialMessage.text;
    string _encryptedMessage = encryptedMessage.text;

    _dbc = new SqliteConnection(_constr);
    _dbc.Open();

    _dbcm = _dbc.CreateCommand();
    _dbcm.CommandText = "CREATE TABLE IF NOT EXISTS previousMessages (ID    INTEGER NOT NULL PRIMARY KEY , Cipher   VARCHAR(5000) NOT NULL, InitialMessage  VARCHAR(5000) NOT NULL,EncryptedMessage TEXT NOT NULL)";
    _dbr = _dbcm.ExecuteReader();
    _dbr.Close();

    _dbcm.CommandText = "INSERT INTO previousMessages ( Cipher, InitialMessage, EncryptedMessage) VALUES ( "+_cipher+", "+_initialMessage+", "+_encryptedMessage+")";
    _dbr = _dbcm.ExecuteReader();
    _dbr.Close();

    _dbcm.CommandText = "SELECT FROM previousMessages (Cipher, InitialMessage, EncryptedMessage) VALUES ( " + _cipher + ", " + _initialMessage + ", " + _encryptedMessage + ")";
    _dbr = _dbcm.ExecuteReader();

    while (_dbr.Read())
    {
        Debug.Log("Cipher: " + _dbr["Cipher"] + "\t Initial: " + _dbr["InitialMessage"]);
    }

}

}

1 Answer 1

2

If you follow OOP you're life will be simpler. I mean you'd rather let a method do only one thing; or if it HAS TO do many things than make it a list of calls to other methods which do only 1 thing each. That being said your AddSQL() breaks down to:

public void AddSQL() {
    string _cipher = cipher.text;
    string _initialMessage = initialMessage.text;
    string _encryptedMessage = encryptedMessage.text;

    OpenConnection();
    ExecuteCreateCommand();
    InsertCommand(_cipher, _initialMessage, _encryptedMessage);
    ReadStuff(_cipher, _initialMessage, _encryptedMessage);
    DebugRead();

}

void OpenConnection(){
    _dbc = new SqliteConnection(_constr);
    _dbc.Open();
}

void ExecuteCreateCommand(){
    _dbcm = _dbc.CreateCommand();
    _dbcm.CommandText = "CREATE TABLE IF NOT EXISTS previousMessages (ID    INTEGER NOT NULL PRIMARY KEY , Cipher   VARCHAR(5000) NOT NULL, InitialMessage  VARCHAR(5000) NOT NULL,EncryptedMessage TEXT NOT NULL)";
    _dbr = _dbcm.ExecuteReader();
    _dbr.Close();
}

void InsertCommand(string _cipher, string _initialMessage, string _encryptedMessage){
    _dbcm.CommandText = "INSERT INTO previousMessages ( Cipher, InitialMessage, EncryptedMessage) VALUES ( "+_cipher+", "+_initialMessage+", "+_encryptedMessage+")";
    _dbr = _dbcm.ExecuteReader();
    _dbr.Close();
}

void ReadStuff(string _cipher, string _initialMessage, string _encryptedMessage){
    _dbcm.CommandText = "SELECT FROM previousMessages (Cipher, InitialMessage, EncryptedMessage) VALUES ( " + _cipher + ", " + _initialMessage + ", " + _encryptedMessage + ")";
    _dbr = _dbcm.ExecuteReader();
}

void DebugRead(){
    while (_dbr.Read())
    {
        Debug.Log("Cipher: " + _dbr["Cipher"] + "\t Initial: " + _dbr["InitialMessage"]);
    }
}

Now in AddSQL() comment all the method calls and uncomment one by one to see which query throws an exception.

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

2 Comments

Thanks for the help; I can be a bit messy at times with my coding. I still keep getting the error that apparently the string "Ceaser" cannot go into my table as "no such column exists". Do you know why this is happening?
The point was to let you know which line gives you an error. But well, I assume in ExecuteCreateCommand() and in InsertCommand() you need to use _dbcm.ExecuteNonQuery() instead of _dbcm.ExecuteReader(). Not sure though. ExecuteNonQuery docs say you can use this for creating database objects such as tables

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.