2

我是 Android 新手,我对 EditText 和 SQLite 有疑问。我想从 EditText 获取字符串,然后使用可点击按钮将其插入数据库......但我的代码不起作用。

这是我的代码:

Button savebtn, clearbtn;
EditText deatxt, numtxt;

public ArrayList<String> num_name, det_name;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    clearbtn = (Button) findViewById(R.id.clearbtn);
    savebtn = (Button) findViewById(R.id.savebtn);
    deatxt = (EditText) findViewById(R.id.deatxt);
    numtxt = (EditText) findViewById(R.id.numtxt);

    clearbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            deatxt.setText("");
            numtxt.setText("");

            savebtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                    Toast.makeText(SarresidActivity.this, "saved!", Toast.LENGTH_LONG).show();

                    DatabaseManager mDatabaseManager = new DatabaseManager(SarresidActivity.this);

                    SQLiteDatabase mainDatabase = mDatabaseManager.getDatabase();

                    String deatxtstring = deatxt.getText().toString();
                    String numtxtstring = numtxt.getText().toString();

                    mainDatabase.execSQL("INSERT INTO tbl_name(num_name, det_name) VALUES ('" + numtxtstring + "','" + deatxtstring + "');");

                    Cursor mCursor = mainDatabase.rawQuery("SELECT * FROM tbl_name;", null);

                    num_name = new ArrayList<String>();
                    det_name = new ArrayList<String>();

                    if (mCursor.moveToFirst()) {
                        // SQL data
                        do {
                            num_name.add(mCursor.getString(mCursor.getColumnIndex("num_name")));
                            det_name.add(mCursor.getString(mCursor.getColumnIndex("det_name")));
                        }
                        while (mCursor.moveToNext());
                    }

                    mCursor.close();

                    mDatabaseManager.closeDatabase();

                }

和 :

 public class DatabaseManager {

public final static String DATABASE_NAME       = "sarresid.db";
public final static String DATABASE_TABLE_NAME = "tbl_name";

private Context            mContext;

private SQLiteDatabase     mSQLiteDatabase;


public DatabaseManager(Context c) {
    mContext = c;

    init();
}


private void init() {
    mSQLiteDatabase = mContext.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);

    mSQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS "
            + " tbl_name (id INTEGER"
            + ", num_name number"
            + ", det_name VARCHAR"
            + ", PRIMARY KEY(id));");
}


public SQLiteDatabase getDatabase() {
    return mSQLiteDatabase;
}


public void closeDatabase() {
    if (mSQLiteDatabase != null)
        mSQLiteDatabase.close();
}
4

2 回答 2

1

我还没有尝试过您创建和访问 SQLite 数据库的方式。

我通常这样做的方式是创建一个扩展SQLiteOpenHelper的类

onCreate()方法中,您可以像这样编写创建表 sql 语句:(这些常量是为了以后更容易访问,它们也有助于防止拼写错误)

private static final String TABLE_HOUSE = "house";
private static final String COLUMN_HOUSE_ID = "_id";
private static final String COLUMN_HOUSE_STREET = "street";

[...]

public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_HOUSE + " (" 
        + COLUMN_HOUSE_ID + " integer primary key autoincrement, " 
        + COLUMN_HOUSE_STREET + " varchar(100))");
}

您还需要创建自己的方法来插入或获取数据。

public HouseCursor queryHouse() {
    Cursor wrapped = getReadableDatabase()
        .query(TABLE_HOUSE, null, null, null, null, null, null);
    return new HouseCursor(wrapped);
}

如果要对结果进行 ig 分组或排序,则需要用 sql 代码替换相应的 null。(在这里查看信息)

在我的示例中,我将结果放在 CursorWrapper 中,如果您想在 ListView 中查看数据,可以将其放入 CursorAdapter

public static class HouseCursor extends CursorWrapper {

    public HouseCursor(Cursor cursor) {
        super(cursor);
    }

    public House getHouse() {
    if (isBeforeFirst() || isAfterLast())
        return null;

        House h = new House();
        h.setId(getLong(getColumnIndex(COLUMN_HOUSE_ID)));
        h.setStreet(getString(getColumnIndex(COLUMN_HOUSE_STREET)));

        return h;
    }
}

可以这样插入数据

public long insertHouse(House h) {
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_HOUSE_STREET, h.getStreet());

    return getWritableDatabase().insert(TABLE_HOUSE, null, cv);
}

我从中获取源代码的类和整个应用程序是here@github

于 2013-09-08T21:15:07.680 回答
0

即使它试图插入,id参数呢?

注意:主键必须具有非空且唯一的值

于 2013-09-08T16:02:06.540 回答