这是数据库文件:
public class MyDBHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION=1;
public static final String DATABASE_NAME="mydetails.db";
public static final String TABLE_MYDETAIL="mydetails";
public static final String COLUMN_NAME="_name";
public static final String COLUMN_ROLLNUMBER="_rollNumber";
public static final String COLUMN_MARKS="_marks";
public static final String COLUMN_ID="_id";
public MyDBHandler(Context context,String name, SQLiteDatabase.CursorFactory cursorFactory,int version){
super(context,DATABASE_NAME,cursorFactory,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query ="CREATE TABLE "+TABLE_MYDETAIL+"("+COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+COLUMN_ROLLNUMBER+" INTEGER, "+ COLUMN_NAME+" TEXT, "+COLUMN_MARKS +" INTEGER"+");";
Log.d("query",query);
db.execSQL(query);
}
public void addMyDetail(MyDetails myDetails){
ContentValues values= new ContentValues();
values.put(COLUMN_ROLLNUMBER,myDetails.get_rollNumber());
values.put(COLUMN_NAME,myDetails.get_name());
values.put(COLUMN_MARKS,myDetails.get_marks());
SQLiteDatabase db = getReadableDatabase();
long id= db.insert(TABLE_MYDETAIL, null, values);
Log.d("id", "addMyDetail: "+id);
db.close();
}
public void deleteMyDetail(String myName){
SQLiteDatabase db = getReadableDatabase();
db.execSQL("DELETE FROM " + TABLE_MYDETAIL + " WHERE " + COLUMN_NAME + "=\"" + myName + "\";");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+TABLE_MYDETAIL);
onCreate(db);
}
public String databaseToString(){
String dbString="";
SQLiteDatabase db =getWritableDatabase();
String query="SELECT * FROM " + TABLE_MYDETAIL + " WHERE 1" ;
Cursor c =db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex("_marks"))!=null){
dbString+= c.getString(c.getColumnIndex(COLUMN_ROLLNUMBER));
dbString+=c.getString(c.getColumnIndex(COLUMN_NAME));
dbString+=c.getString(c.getColumnIndex(COLUMN_MARKS));
dbString += "\n";
}
c.moveToNext();
}
c.close();
db.close();
return dbString;
}
}
这是我的MainActivity
package com.example.saranshagarwal.studentdetailsapp;
public class MainActivity extends AppCompatActivity {
EditText name,rollnumber,marks;
MyDBHandler myDBHandler;
TextView tv2;
final Context context=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.editText2);
rollnumber = (EditText) findViewById(R.id.editText5);
marks = (EditText) findViewById(R.id.editText4);
tv2=(TextView)findViewById(R.id.textView5);
myDBHandler=new MyDBHandler(this,null,null,1);
}
public void addButtonClicked(View view){
MyDetails myDetails=new MyDetails();
myDetails.set_rollNumber(rollnumber.getText().toString());
myDetails.set_name(marks.getText().toString());
myDetails.set_marks(marks.getText().toString());
myDBHandler.addMyDetail(myDetails);
String dbString= myDBHandler.databaseToString();
tv2.setText(dbString);
name.setText("");
rollnumber.setText("");
marks.setText("");
}
public void deleteButtonClicked(View view) {
String inputText = name.getText().toString();
myDBHandler.deleteMyDetail(inputText);
}
public void onViewClicked(View view){
Dialog dialog= new Dialog(context);
dialog.setContentView(R.layout.popup_on_view);
TextView tv1=(TextView)findViewById(R.id.popup_text);
String dbString= myDBHandler.databaseToString();
tv1.setText(dbString);
dialog.show();
}
}
答案 0 :(得分:0)
您正在代码中添加标记代替名称
将您的代码更改为此
myDetails.set_rollNumber(rollnumber.getText().toString());
myDetails.set_name(name.getText().toString());
myDetails.set_marks(marks.getText().toString());
可能原因是您访问数据库的方式。 制作一个像这样的全局变量 -
private SQLiteDatabase db;
在您的构造函数MyDBHandler()
中对此进行初始化
db=context.openOrCreateDatabase(DATABASE_NAME,DATABASE_VERSION,null);
然后在每个后续方法中使用它。
此外,您需要通过在MyDBHandler
类中创建方法来显式关闭数据库。
public void close() throws SQLException {
db.close();
}
一旦完成db的工作,就调用它。从其他方法中删除db.close()
语句。
此外,您需要在活动中为每个方法实现此功能
public void addButtonClicked(View view){
MyDetails myDetails=new MyDetails();
myDetails.set_rollNumber(rollnumber.getText().toString());
myDetails.set_name(marks.getText().toString());
myDetails.set_marks(marks.getText().toString());
myDBHandler=new MyDBHandler(this,null,null,1);
myDBHandler.addMyDetail(myDetails);
String dbString= myDBHandler.databaseToString();
myDBHandler.close();
tv2.setText(dbString);
name.setText("");
rollnumber.setText("");
marks.setText("");
}
希望这会有所帮助:)