我正在尝试使用VarChar列的字符串变量向表中添加记录。我想在下一个递增的ID下插入我给它的值。但是,当我尝试运行代码时,我不断收到“没有这样的列存在”的SQL错误。没有详细说明错误发生的位置,所以我有点陷入困境。那么如何将记录插入表中呢?
这是我的代码
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"]);
}
}
}
答案 0 :(得分:2)
如果你遵循OOP,你的生活会更简单。我的意思是你宁愿让方法只做一件事;或者如果它做了许多事情而不是使它成为一个调用其他方法的列表,每个方法只做一件事。据说你的AddSQL()
分解为:
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"]);
}
}
现在在AddSQL()
注释所有方法调用并逐个取消注释,以查看哪个查询引发异常。