使用android sqlite中的item存储和获取数据

时间:2013-07-29 21:46:44

标签: android database sqlite android-sqlite

我正在制作医疗商店数据库应用程序...... 其中我想存储药品的名称,价格,公司,日期和说明......

我试图使用Item ...

存储所有数据

我使用此代码保存数据...

TextView tv;
    tv = (TextView) findViewById(R.id.title);
    item.setName(tv.getText().toString());
    tv = (TextView) findViewById(R.id.content);
    item.setNotecontent(tv.getText().toString());
    tv = (TextView) findViewById(R.id.editTextPrice);
    item.setNotecontent(tv.getText().toString());
    tv = (TextView) findViewById(R.id.editTextCompany);
    item.setNotecontent(tv.getText().toString());
    tv = (TextView) findViewById(R.id.editTextDat);
    item.setNotecontent(tv.getText().toString());

此代码获取

item = (Item) getIntent().getSerializableExtra("item");
    TextView t = (TextView) findViewById(R.id.title);
    t.setText(item.getName());
    t = (TextView) findViewById(R.id.content);
    t.setText(item.getNotecontent());
    t = (TextView) findViewById(R.id.company);
    t.setText(item.getNotecontent());
    t = (TextView) findViewById(R.id.price);
    t.setText(item.getNotecontent());
    t = (TextView) findViewById(R.id.date);
    t.setText(item.getNotecontent());

但它只显示Name并忽略所有其他数据...... 因为我这是因为getName()和getNotecontent()... 请帮我解决一下

这是数据库:

package com.placella.todo;
import java.util.ArrayList;
import java.util.List;
import android.content.*;
import android.database.Cursor;
import android.database.sqlite.*;

public class Db extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;

public Db(Context context) {
    super(context, "todo", null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(
        "CREATE TABLE list"
        + "("
            + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
            + "name TEXT,"
            + "type TEXT,"
            + "note TEXT,"
            + "company TEXT,"
            + "price TEXT,"
            + "date TEXT,"
            + "lid INTEGER" // List items reference
        + ");"
    );
    db.execSQL(
            "CREATE TABLE items"
            + "("
                + "lid INTEGER,"
                + "content TEXT,"
                + "checked INTEGER"
            + ");"
        );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS list;");
    db.execSQL("DROP TABLE IF EXISTS items;");
    onCreate(db);
}

public void delete(Item item) {
    SQLiteDatabase db = this.getWritableDatabase();
    if (item.getType() == Item.LIST) {
        String query = "SELECT lid FROM list WHERE id = '" + item.getId()+ "'";
        Cursor cursor = db.rawQuery(query, null);
        long lid;
        if (cursor.moveToFirst()) {      
            lid = cursor.getInt(0) + 1;                  
        } else {
            lid = -1;
        }
        cursor.close();
        db.execSQL("DELETE FROM items WHERE lid = '" + lid  + "';");
    }
    db.execSQL("DELETE FROM list WHERE id = '" + item.getId()  + "';");
}

public long add(Item item) throws Exception {
    long id, lid = -1, retval;
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("name", item.getName());
    values.put("type", item.getType());
    values.put("note", item.getNotecontent());
    values.put("company", item.getNotecontent());
    values.put("price", item.getName());
    values.put("date", item.getNotecontent());

    if (item.getType() != Item.NOTE) {
        lid = this.getNextListTableId();
        values.put("lid", lid);
    }
    id = db.insert("list", null, values);
    if (id == -1) {
        throw new Exception("Can't insert into db.items");
    } else {
        item.setId(id);
    }
    if (item.getType() == Item.LIST) {
        for (Item i : item.getListcontent()) {
            values = new ContentValues();
            values.put("lid", lid);
            values.put("content", i.getName());
            values.put("checked", i.getState());
            retval = db.insert("items", null, values);
            if (retval == -1) {
                throw new Exception("Can't insert into db.list");
            }
        }
    }
    return id;
}

private long getNextListTableId() {
    String query = "SELECT MAX(lid) FROM items";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    long id;
    if (cursor.moveToFirst()) {      
        id = cursor.getInt(0) + 1;                  
    } else {
        id = 0;
    }
    cursor.close();
    return id;
}

public List<Item> getList() {
    List<Item> list = new ArrayList<Item>();
    SQLiteDatabase db = this.getReadableDatabase();
    String query = "SELECT id, name, type, note, company, price, date, lid FROM list";
    Cursor c = db.rawQuery(query, null);
    if (c.moveToFirst()) {
        do {
            Item item = new Item(
                c.getString(1),
                c.getInt(2)
            );
            List<Item> innerList = new ArrayList<Item>();
            if (c.getInt(2) == Item.LIST) {
                Cursor cc = db.rawQuery(
                    "SELECT content, checked FROM items WHERE lid = '" + c.getInt(4) + "'",
                    null
                );
                if (cc.moveToFirst()) {      
                    do {
                        innerList.add(
                            new Item(cc.getString(0),         Item.NOTE).setState(cc.getInt(1))
                        );
                    } while (cc.moveToNext());
                }
                cc.close();
            }
            item.setNotecontent(c.getString(3));
            item.setId(c.getInt(0));
            list.add(item);
            item.setListcontent(innerList);
        } while (c.moveToNext());
    }
    c.close();
    return list;
}
}

0 个答案:

没有答案