使用Android eclipse将值添加到SQLite中

时间:2013-09-23 08:44:59

标签: java android sqlite

我是Android开发的新手。我正在尝试在sqllite中创建一个数据库表并为其添加值。我使用以下代码

add.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
   <EditText
    android:id="@+id/nametxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10" >
    <requestFocus />
</EditText>
 <EditText
    android:id="@+id/notxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10" >
  </EditText>
  <Button
    android:id="@+id/addcontactBtn"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:onClick="addContacts"
   android:text="Add New" />
</LinearLayout>

DataBase.java

  public class DataBase extends SQLiteOpenHelper {
public static final String DETAILS = "Details";
public static final String DETAIL_ID = "_id";
public static final String DETAIL_NAME = "_name";
public static final String DETAIL_NO="_no";

private static final String DATABASE_NAME = "Details.db";
private static final int DATABASE_VERSION = 1;
// creation SQLite statement
private static final String DATABASE_CREATE = "create table " + DETAILS
        + "(" + DETAIL_ID + " integer primary key autoincrement, "
        + DETAIL_NAME +" text not null"+ DETAIL_NO + " text not null);";

public DataBase(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DATABASE_CREATE);

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

}

Detail.java

  public class Detail {
private int id;
private String name;
private String number;
public long getId() {return id; }
public void setId(int id) {this.id = id;}
public String getName() {return this.name;}
public void setName(String name) {this.name = name;}
public String getNumber(){return this.number;   }
public void setnumber(String number){   this.number=number; }
  }

DetailOperations.java

    public class DetailOperations {
private DataBase dbHelper;
private String[] DETAILS_TABLE_COLUMNS = { DataBase.DETAIL_ID, DataBase.DETAIL_NAME,DataBase.DETAIL_NO };
private SQLiteDatabase database;
public DetailOperations(Context context) {
    dbHelper = new DataBase(context);
}
public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();  }

    public void close() {       dbHelper.close();   }

public Detail addDetail(String name,String number) {
    ContentValues values = new ContentValues();
    values.put(DataBase.DETAIL_NAME, name);
    values.put(DataBase.DETAIL_NO, number);
    long detId = database.insert(DataBase.DETAILS, null, values);
    Cursor cursor = database.query(DataBase.DETAILS,
            DETAILS_TABLE_COLUMNS, DataBase.DETAIL_ID + " = "
                    + detId, null, null, null, null);
    cursor.moveToFirst();
    Detail newdetail = parseDetail(cursor);
    cursor.close();
    return newdetail;
}
private Detail parseDetail(Cursor cursor) {
    Detail d = new Detail();
    d.setId((cursor.getInt(0)));
    d.setName(cursor.getString(1));
    d.setnumber(cursor.getString(2));
    return d;
}         }

Add.java

    public class Add extends Activity {
private DetailOperations detailDBoperation;
@Override
public void onCreate(Bundle savedInstaceState){
    super.onCreate(savedInstaceState);
    setContentView(R.layout.add);
}
   public void addDetails(View view){
    detailDBoperation=new DetailOperations(this);
    detailDBoperation.open();
    EditText name=(EditText) findViewById(R.id.nametxt);
    EditText number=(EditText) findViewById(R.id.notxt);
    detailDBoperation.addDetail(name.getText().toString(), number.getText().toString());
    name.setText("");
    number.setText("");

}}

当我按下“添加新”按钮时,我收到了错误,不幸的是App已经停止并显示上一页。我检查DDMS它显示数据库表已创建,但值未添加到表中。

LogCat显示

09-23 04:50:17.914: E/AndroidRuntime(10574): java.lang.IllegalStateException: 
Could not find a method addContacts(View) in the activity class com.example.experiments.Add for onClick handler on view class android.widget.Button with id 'addcontactBtn'

3 个答案:

答案 0 :(得分:2)

您想要调用方法addContactsandroid:onClick="addContacts"),并在活动中将其称为addDetails(View view)

使用public void addDetails(View view)

更改public void addContacts(View view)

答案 1 :(得分:1)

将以下方法/方法签名public void addDetails(View view)替换为public void addContacts(View view),因为这是布局XML配置文件中定义的方法:

<Button
    android:id="@+id/addcontactBtn"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:onClick="addContacts" <!-- here -->
    android:text="Add New" />

答案 2 :(得分:1)

确保您已包含onClick for按钮:

<Button
    android:id="@+id/addcontactBtn"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:onClick="addContacts"
   android:text="Add New" 
android:onClick="addDetails"/>

将查询更改为:在查询

中不为null后丢失','
 private static final String DATABASE_CREATE = "create table " + DETAILS
       + "(" + DETAIL_ID + " integer primary key autoincrement, "
       + DETAIL_NAME +" text not null, "+ DETAIL_NO + " text not null);";