当值“”时,无法从表中的列获取数据

时间:2012-04-16 15:52:59

标签: sql null

当我从表格中的列中获取价值时,我的项目中有一个问题。我用c#代码创建数据库。

这是我的代码:

    public static int typeTransactionIncome = 1; // thu
    public static int typeTransactionExpense = 2; // chi
    public static int typeTransactionDebt = 3; // nợ
    public static int typeTransactionLoan = 4; // cho vay

    public static string tableNameCategory = "categories";

    private static string createTableTransactionString = "CREATE TABLE IF NOT EXISTS \"transactions\" " + "(\"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"
        + "\"name\" VARCHAR(140) ," + "\"amount\" FLOAT NOT NULL  DEFAULT (0) ," + "\"type\" INTEGER NOT NULL ,"
        + "\"created_date\" DATETIME NOT NULL  DEFAULT (CURRENT_DATE) ," + "\"displayed_date\" DATETIME NOT NULL  DEFAULT (CURRENT_DATE) ,"
        + "\"cat_id\" INTEGER NOT NULL  DEFAULT (0) ," + "\"with_person\" VARCHAR(50)," + "\"remind_date\" DATETIME,"
        + "\"remind_num\" INTEGER DEFAULT (0) ," + "\"note\" VARCHAR(140) ," + "\"status\" BOOL NOT NULL  DEFAULT (0),"
        + "\"user_id\" INT NOT NULL  DEFAULT (1))";

这是插入代码:插入一些列,然后有2个插入menthod

public void insertNewIncome(string _name, string _note, double _amount, DateTime _displayedDate, int _catId, int _usertId)
    {
        try
        {
            openConnection();

            SqliteCommand cmd = conn.CreateCommand();
            cmd.CommandText = "INSERT INTO " + tableNameTransaction + "(type, name, note, amount, displayed_date, cat_id, user_id) values (@type, @name, @note, @amount, @displayed_date, @cat_id, @user_id)";
            cmd.Parameters.Add("@type", typeTransactionIncome);
            cmd.Parameters.Add("@name", _name);
            cmd.Parameters.Add("@note", _note);
            cmd.Parameters.Add("@amount", _amount);
            cmd.Parameters.Add("@displayed_date", _displayedDate);
            cmd.Parameters.Add("@cat_id", _catId);
            cmd.Parameters.Add("@user_id", _usertId);
            cmd.ExecuteNonQuery();
            //MessageBox.Show("income");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            closeConnection();
        }
    }

public void insertNewDebt(string _debter, string _note, double _amount, DateTime _ngay_vay, DateTime _ngay_tra, int _user_id)
    {
        try
        {
            openConnection();

            SqliteCommand cmd = conn.CreateCommand();
            cmd.CommandText = "INSERT INTO " + tableNameTransaction + "(type, with_person, note, amount, displayed_date, remind_date, user_id) VALUES (@type, @with_person, @note, @amount, @displayed_date, @remind_date, @user_id)";
            cmd.Parameters.Add("@type", typeTransactionDebt);
            cmd.Parameters.Add("@with_person", _debter);
            cmd.Parameters.Add("@note", _note);
            cmd.Parameters.Add("@amount", _amount);
            cmd.Parameters.Add("@displayed_date", _ngay_tra);
            cmd.Parameters.Add("@remind_date", _ngay_tra);
            cmd.Parameters.Add("@user_id", _user_id);
            cmd.ExecuteNonQuery();
            //MessageBox.Show("debt");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            closeConnection();
        }
    }

我是通过insertNewDebt和insertNewIncome menthod将一些记录插入到表中,没关系,没有任何错误。

这是获得表中所有记录的方法:

public List<ItemTransaction> loadTransactionAll(DateTime _start_date, DateTime _end_date, int _user_id)
    {
        List<ItemTransaction> li = new List<ItemTransaction>();
            openConnection();

            SqliteCommand cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT transactions.id AS master_id,transactions.name,transactions.amount,transactions.type,transactions.created_date, transactions.displayed_date,transactions.cat_id, transactions.with_person,transactions.remind_date,remind_num, transactions.note,transactions.status,transactions.user_id,categories.id,categories.name,categories.icon,categories.type,IFNULL(sub_amount,0) AS sub_amount "
                    + "FROM transactions "
                    + "LEFT JOIN categories ON transactions.cat_id = categories.id "
                    + "LEFT JOIN ("
                    + "SELECT sub_trans_id, IFNULL(SUM(sub_amount),0) AS sub_amount "
                    + "FROM (SELECT trans_id AS sub_trans_id,SUM(amount) AS sub_amount "
                    + "FROM sub_transactions "
                    + "WHERE type = @type_income "
                    + "GROUP BY sub_trans_id "
                    + "UNION ALL SELECT trans_id AS sub_trans_id,SUM(amount) * -1 AS sub_amount "
                    + "FROM sub_transactions "
                    + "WHERE type = @type_expense GROUP BY sub_trans_id) GROUP BY sub_trans_id) ON sub_trans_id = master_id "
                    + "WHERE (displayed_date BETWEEN @start_date AND @end_date) "
                    + "AND transactions.user_id = @user_id "
                    + "ORDER BY displayed_date DESC, transactions.type ASC, status ASC";
            cmd.Parameters.Add("@type_income", DatabaseProccess.typeTransactionIncome);
            cmd.Parameters.Add("@type_expense", DatabaseProccess.typeTransactionExpense);
            cmd.Parameters.Add("@start_date", _start_date);
            cmd.Parameters.Add("@end_date", _end_date);
            cmd.Parameters.Add("@user_id", _user_id);

            SqliteDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ItemTransaction item = new ItemTransaction();
                item.master_id = reader.GetInt32(0);
                item.name = reader.GetString(1);
                item.amount = reader.GetDouble(2);
                item.type = reader.GetInt32(3);
                item.created_date = reader.GetDateTime(4);
                item.displayed_date = reader.GetDateTime(5);
                item.cat_id = reader.GetInt32(6);
                item.with_person = reader.GetString(7);
                item.remind_date = reader.GetDateTime(8);
                item.remind_num = reader.GetInt32(9);
                item.note = reader[10].ToString();
                item.status = reader.GetBoolean(11);
                item.user_id = reader.GetInt32(12);
                item.setCategory(reader.GetInt32(13), reader.GetString(14), reader.GetString(15), reader.GetInt32(16));
                item.sub_amount = reader.GetDouble(17);

                li.Add(item);
            }

        return li;
    }

但是当我执行insert:

insertNewIncome("", "note", "1000", DateTime.Today, 1, 1);

使用'name'字段值=“”

我无法读取'name'字段。调试说:

view error screen shot here, please

这是ItemTransaction类:

view ItemTransaction class here, please

我不明白这个错误。

拜托,谢谢

1 个答案:

答案 0 :(得分:0)

您收到此错误是因为name为NULL。

以下if语句将解决此问题:

if (reader(1) != null){
  item.name = reader.GetString(1);
}

但是,如果您传递的值为"",则不应将其存储为NULL。

检查您的数据库并查看其值。也许您有一个触发器可以将""更新为null