SQLite插入行会自动删除

时间:2016-04-15 21:21:58

标签: c# wpf sqlite

我在WPF Visual C#中将数据插入SQLIte数据库。 insert命令工作正常。我通过返回结果中的总行数来检查函数,并显示插入的行。但是当我检查所有数据时,它什么也没显示。我也尝试了总计数,它显示为0。

以下是我的数据库控制器的代码:

class TableController
{
    SQLiteConnection m_dbConnection;

    public TableController()
    {
        SQLiteConnection.CreateFile("MyDatabase.sqlite");
        m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
        createDb();

    }

    public void createDb()
    {

        m_dbConnection.Open();
        string sql = "CREATE TABLE data (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(50), address VARCHAR(100), mobile VARCHAR(20), date VARCHAR(20), price VARCHAR(20), warranty VARCHAR(20), month VARCHAR(20))";
        SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
        command.ExecuteNonQuery();
        m_dbConnection.Close();
    }

    public Boolean add_data(Objectdata od)
    {
            m_dbConnection.Open();
            string sql = "INSERT INTO data (name, address, mobile, date, price, warranty, month) values ('" + od.name + "', '" + od.address + "', '" + od.mobile + "', '" + od.date + "', '" + od.price + "', '" + od.warranty + "', '" + od.month + "')";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            int a = command.ExecuteNonQuery();
            m_dbConnection.Close();

            if (a == 0)
            {
                return false;
            }
            else if (a == 1)
            {
                return true;
            }
            else
            {
                return false;
            }

    }


    public List<Objectdata> getAllData()
    {

        m_dbConnection.Open();
        List<Objectdata> dataList = new List<Objectdata>();
        Objectdata oD = new Objectdata();

        string sql = "select * from data order by id";
        SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
        SQLiteDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            oD.setId(reader["id"].ToString());
            oD.setName(reader["name"].ToString());
            oD.setAddress(reader["address"].ToString());
            oD.setMobile(reader["mobile"].ToString());
            oD.setDate(reader["date"].ToString());
            oD.setPrice(reader["price"].ToString());
            oD.setWarranty(reader["warranty"].ToString());
            oD.setMonth(reader["month"].ToString());

            dataList.Add(oD);

        }
        m_dbConnection.Close();


        return dataList;
    }

    public Objectdata getSingleData(String id)
    {

        m_dbConnection.Open();

        Objectdata oD = new Objectdata();

        string sql = "select * from data WHERE id = '" + id + "'";
        SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
        SQLiteDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            oD.setId(reader["id"]);
            oD.setName(reader["name"]);
            oD.setAddress(reader["address"]);
            oD.setDate(reader["date"]);
            oD.setMobile(reader["mobile"]);
            oD.setPrice(reader["price"]);
            oD.setWarranty(reader["warranty"]);

        }
        m_dbConnection.Close();

        return oD;

    }

    public int getCount()
    {

        m_dbConnection.Open();

        int count = 0;
        string sql = "SELECT * FROM data";
        SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
        SQLiteDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            count++;
        }
        m_dbConnection.Close();
        return count;
    }

我正在通过这门课程访问数据。

 TableController tablecon = new TableController();

            Objectdata ob = new Objectdata();
            ob.name = name.Text.ToString();
            ob.address = address.Text.ToString();
            ob.mobile = mobile.Text.ToString();
            ob.date = date.Text.ToString();
            ob.price = price.Text.ToString();
            ob.warranty = warranty.Text.ToString();
            ob.month = mc.returnMonth(date.Text.ToString());
            Boolean res = tablecon.add_data(ob);

            if (res)
            {
                MessageBox.Show("Added");
                name.Clear();
                name.Clear();
                address.Clear();
                mobile.Clear();
                price.Clear();
                warranty.Clear();
            }
            else
            {
                MessageBox.Show("Sorry an error occurred!");
            }

      private void button_refresh_Click(object sender, RoutedEventArgs e)
    {
        TableController a = new TableController();
        dataList = a.getAllData();
        listView.ItemsSource = dataList;
    }

检查计数的一个类(它显示为零):

 public void getId(String id)
    {
        this.id = id;
        load_data(id);
    }

    public void load_data(String id)
    {
        Objectdata o = new Objectdata();
        o = tableContoller.getSingleData(id);
        name.AppendText(tableContoller.getCount().ToString());
        address.AppendText(o.getAddress());
        mobile.AppendText(o.getMobile());
        date.AppendText(o.getDate());
        price.AppendText(o.getPrice());
        warranty.AppendText(o.getWarranty());
    }

1 个答案:

答案 0 :(得分:1)

您的CreateDB()方法令人困惑,因为它会尝试重新创建实际应该抛出异常的表data。无论如何,从构造函数中删除该方法调用。看到你的构造函数......

   public TableController()
    {
        SQLiteConnection.CreateFile("MyDatabase.sqlite");
        m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
        createDb(); <-- this line

    }