问题描述
在此项目中的所有功能,做工不错接受删除,并显示所有数据。
一旦如果我删除的记录,如果我要插入,然后它会插入,但我不能看到它之后。我认为这是一些previous指标值后插入。所以,请告诉我,我错了,删除所有记录和显示所有记录。
我使用光标它。我不知道如何删除和光标显示所有的记录。请给我你的观点,我在哪里错了。
该项目code是在这里:
包com.example.databaseExample2;
进口android.content.ContentValues;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.util.Log;公共类DBAdapter
{
公共静态最后弦乐KEY_ROWID =_id;
公共静态最后弦乐KEY_ISBN =ISBN;
公共静态最后弦乐KEY_TITLE =称号;
公共静态最后弦乐KEY_PUBLISHER =出版人;
私有静态最后弦乐TAG =DBAdapter; 私有静态最后弦乐DATABASE_NAME =测试;
私有静态最后弦乐DATABASE_TABLE =标题;
私有静态最终诠释DATABASE_VERSION = 1; 私有静态最后弦乐DATABASE_CREATE =
创建表的标题(_id整数主键自动增量
+ISBN文本不为空,标题文本不为空,
+出版文字不是null);; 私人最终上下文的背景下; 私人DatabaseHelper DBHelper;
私人SQLiteDatabase分贝; 公共DBAdapter(上下文CTX)
{
this.context = CTX;
DBHelper =新DatabaseHelper(背景);
} 私有静态类DatabaseHelper扩展SQLiteOpenHelper
{
DatabaseHelper(上下文的背景下)
{
超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
} @覆盖
公共无效的onCreate(SQLiteDatabase DB)
{
db.execSQL(DATABASE_CREATE);
} @覆盖
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,
INT静态网页)
{
Log.w(TAG,从版本升级数据库+ oldVersion +到+ NEWVERSION +,这将摧毁所有旧数据);
db.execSQL(DROP TABLE IF EXISTS标题);
的onCreate(DB);
}
} // ---打开数据库---
公共DBAdapter的open()抛出的SQLException
{
DB = DBHelper.getWritableDatabase();
返回此;
} // ---关闭数据库---
公共无效的close()
{
DBHelper.close();
} // ---插入标题到数据库---
众长insertTitle(ISBN字符串,字符串title,字符串出版社)
{
ContentValues initialValues =新ContentValues();
initialValues.put(KEY_ISBN,ISBN);
initialValues.put(KEY_TITLE,职称);
initialValues.put(KEY_PUBLISHER,发布者);
返回db.insert(DATABASE_TABLE,空,initialValues);
} // ---删除特定的书名---
公共布尔deleteTitle(长ROWID)
{
返回db.delete(DATABASE_TABLE,KEY_ROWID +=+ ROWID,NULL)> 0;
}//公共布尔deleteAllTitle()
// {
//返回db.delete(DATABASE_TABLE,NULL,NULL);
//} // ---检索所有的标题---
公共光标getAllTitles()
{
光标mCursor = db.query(DATABASE_TABLE,新的String [] {
KEY_ROWID,
KEY_ISBN,
KEY_TITLE,
KEY_PUBLISHER},
空值,
空值,
空值,
空值,
空值);
如果(mCursor!= NULL)
{
mCursor.moveToFirst();
}
返回mCursor;
} // ---检索特定的书名---
公共光标的getTitle(长ROWID)抛出的SQLException
{
光标mCursor =
db.query(真,DATABASE_TABLE,新的String [] {
KEY_ROWID,
KEY_ISBN,
KEY_TITLE,
KEY_PUBLISHER
},
KEY_ROWID +=+ ROWID,
空值,
空值,
空值,
空值,
空值);
如果(mCursor!= NULL)
{
mCursor.moveToFirst();
}
返回mCursor;
}
公共无效deleteAll()
{
this.db.delete(DATABASE_TABLE,NULL,NULL);
} // ---更新一个标题---
公共布尔updateTitle(长ROWID,字符串书号,
标题字符串,字符串出版社)
{
ContentValues ARGS =新ContentValues();
args.put(KEY_ISBN,ISBN);
args.put(KEY_TITLE,职称);
args.put(KEY_PUBLISHER,发布者);
返回db.update(DATABASE_TABLE,ARGS,KEY_ROWID +=+ ROWID,NULL)> 0;
}
}
和另一个文件是在这里:
包com.example.databaseExample2;进口android.app.Activity;
进口android.database.Cursor;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;公共类DatabaseActivity延伸活动{
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
//定义资源
最终按钮display_btn =(按钮)findViewById(R.id.test_button);
最终的EditText数=(EditText上)findViewById(R.id.number);
最终按钮插入=(按钮)findViewById(R.id.insert);
最终按钮删除=(按钮)findViewById(R.id.delete);
最终按钮allDisplay =(按钮)findViewById(R.id.all); 最后DBAdapter DB =新DBAdapter(本); //创建新的数据库 allDisplay.setOnClickListener(新View.OnClickListener()
{ 公共无效的onClick(视图v)
{
db.open();
光标C = db.getAllTitles();
如果(c.moveToLast())
{
//对于(INT I = 0; c.getCount()大于0;我+ +)
// {
DISPLAYTITLE(C);
//}
}
其他
{
Toast.makeText(getApplicationContext(),找不到标题,Toast.LENGTH_LONG).show();
}
db.close(); }
}); delete.setOnClickListener(新View.OnClickListener()
{ 公共无效的onClick(视图v)
{
db.open();
光标C = db.getAllTitles();
如果(c.getCount()大于0)
{
db.deleteAll();
}
db.close();
的System.out.println(记录删除。);
Toast.makeText(getApplicationContext(),记录删除,Toast.LENGTH_SHORT).show();
}
});
insert.setOnClickListener(新View.OnClickListener()
{ 公共无效的onClick(视图v)
{
// ---加2个标题---
db.open();
长ID; ID = db.insertTitle(128,C-编程,Dhiren);
ID = db.insertTitle(256,机器人,Shreyash);
ID = db.insertTitle(512,机器人,Kandarp);
ID = db.insertTitle(1024,机器人,Sweta); db.close();
的System.out.println(记录插入);
Toast.makeText(getApplicationContext(),记录插入,Toast.LENGTH_SHORT).show(); }
});
display_btn.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图v)
{
// ---获得一个标题---
//字符串ID1 =的ToString();
如果(number.getText()。的toString()。等于())
{
Toast.makeText(getApplicationContext(),请输入任何值......,Toast.LENGTH_LONG).show();
}
其他
{ 整数ID =(整数)的Integer.parseInt(number.getText()的toString());
//System.out.println(\"The ID为:+身份证);
db.open(); 光标C = db.getTitle(ID); 如果(c.moveToFirst())
{
DISPLAYTITLE(C);
}
其他
{
Toast.makeText(getApplicationContext(),找不到标题,Toast.LENGTH_LONG).show();
}
db.close();
}
}
}); } 公共无效DISPLAYTITLE(光标C)
{
Toast.makeText(这一点,
ID:+ c.getString(0)+\\ n+ISBN:+ c.getString(1)+\\ n+TITLE:+ c.getString(2)+\\ n +
PUBLISHER:+ c.getString(3),Toast.LENGTH_LONG).show();
}
公共无效DisplayAll(光标C)
{
的for(int i = 0; c.getCount()= 0;!我++)
{
Toast.makeText(这一点,
ID:+ c.getString(0)+\\ n+ISBN:+ c.getString(1)+\\ n+TITLE:+ c.getString(2)+\\ n +
PUBLISHER:+ c.getString(3),Toast.LENGTH_LONG).show();
c.moveToNext();
}
}
}
请帮我对此。
allDisplay.setOnClickListener(新View.OnClickListener()
{ 公共无效的onClick(视图v)
{
db.open();
光标C = db.getAllTitles();
而(c.moveToNext())
{
Toast.makeText(DatabaseActivity.this,c.getString(0),Toast.LENGTH_LONG).show();
}
}
});
更新
当你需要删除所有不需要检查其他option.Just必须调用删除
delete.setOnClickListener(新View.OnClickListener()
{
公共无效的onClick(视图v)
{
db.open(); db.deleteAll();
db.close();
的System.out.println(记录删除。);
Toast.makeText(getApplicationContext(),记录删除,Toast.LENGTH_SHORT).show();
}
});
更新
公共光标getAllTitles()
{
光标mCursor = db.query(DATABASE_TABLE,新的String [] {
KEY_ROWID,
KEY_ISBN,
KEY_TITLE,
KEY_PUBLISHER},
空值,
空值,
空值,
空值,
空值); 返回mCursor;
}
更新
您已经使用的SQLite自动递增field.So直到超过records.If您将在下一次的ID会被11.It将不会从1.See启动已删除10最大value.Suppose它会永远上升文档
In this project all the functionality works good accept delete and display all data.Once if i delete the record and after that if I am going to insert it, then it will inserted but I am not able to see it. I think it is inserted after some previous index value. So please tell me where I am wrong to delete all the records and display all records.I am using cursor for it. and I don't know how to delete and display all the record by cursor. Please give me your view that where I am wrong.The project code is here :
package com.example.databaseExample2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_ISBN = "isbn";
public static final String KEY_TITLE = "title";
public static final String KEY_PUBLISHER = "publisher";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "test";
private static final String DATABASE_TABLE = "titles";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table titles (_id integer primary key autoincrement, "
+ "isbn text not null, title text not null, "
+ "publisher text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(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)
{
Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion+", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a title into the database---
public long insertTitle(String isbn, String title, String publisher)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ISBN, isbn);
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_PUBLISHER, publisher);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular title---
public boolean deleteTitle(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
// public boolean deleteAllTitle()
// {
// return db.delete(DATABASE_TABLE,null, null);
// }
//---retrieves all the titles---
public Cursor getAllTitles()
{
Cursor mCursor = db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_ISBN,
KEY_TITLE,
KEY_PUBLISHER},
null,
null,
null,
null,
null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_ISBN,
KEY_TITLE,
KEY_PUBLISHER
},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
public void deleteAll()
{
this.db.delete(DATABASE_TABLE, null, null);
}
//---updates a title---
public boolean updateTitle(long rowId, String isbn,
String title, String publisher)
{
ContentValues args = new ContentValues();
args.put(KEY_ISBN, isbn);
args.put(KEY_TITLE, title);
args.put(KEY_PUBLISHER, publisher);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
And another file is here:
package com.example.databaseExample2;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class DatabaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// define resources
final Button display_btn = (Button)findViewById(R.id.test_button);
final EditText number = (EditText)findViewById(R.id.number);
final Button insert = (Button)findViewById(R.id.insert);
final Button delete = (Button)findViewById(R.id.delete);
final Button allDisplay = (Button)findViewById(R.id.all);
final DBAdapter db = new DBAdapter(this); // create new database
allDisplay.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
db.open();
Cursor c = db.getAllTitles();
if (c.moveToLast())
{
// for(int i=0;c.getCount()>0;i++ )
// {
DisplayTitle(c);
//}
}
else
{
Toast.makeText(getApplicationContext(), "No title found", Toast.LENGTH_LONG).show();
}
db.close();
}
});
delete.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
db.open();
Cursor c = db.getAllTitles();
if(c.getCount()>0)
{
db.deleteAll();
}
db.close();
System.out.println("Record Delete.");
Toast.makeText(getApplicationContext(), "Record Delete.", Toast.LENGTH_SHORT).show();
}
});
insert.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//---add 2 titles---
db.open();
long id;
id = db.insertTitle("128","C-Programming","Dhiren");
id = db.insertTitle("256","Android","Shreyash");
id = db.insertTitle("512","Android","Kandarp");
id = db.insertTitle("1024","Android","Sweta");
db.close();
System.out.println("Record Inserted.");
Toast.makeText(getApplicationContext(), "Record Inserted.", Toast.LENGTH_SHORT).show();
}
});
display_btn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//---get a title---
//String id1 = .toString();
if(number.getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(), "Please Enter Any Value...", Toast.LENGTH_LONG).show();
}
else
{
Integer id = (Integer)Integer.parseInt(number.getText().toString());
//System.out.println("The Id is: "+id);
db.open();
Cursor c = db.getTitle(id);
if (c.moveToFirst())
{
DisplayTitle(c);
}
else
{
Toast.makeText(getApplicationContext(), "No title found", Toast.LENGTH_LONG).show();
}
db.close();
}
}
});
}
public void DisplayTitle(Cursor c)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" + "ISBN: " + c.getString(1) + "\n" + "TITLE: " + c.getString(2) + "\n" +
"PUBLISHER: " + c.getString(3),Toast.LENGTH_LONG).show();
}
public void DisplayAll(Cursor c)
{
for(int i = 0; c.getCount()!=0;i++)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" + "ISBN: " + c.getString(1) + "\n" + "TITLE: " + c.getString(2) + "\n" +
"PUBLISHER: " + c.getString(3),Toast.LENGTH_LONG).show();
c.moveToNext();
}
}
}
Please help me regarding this.
allDisplay.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
db.open();
Cursor c = db.getAllTitles();
while(c.moveToNext())
{
Toast.makeText(DatabaseActivity.this,c.getString(0),Toast.LENGTH_LONG).show();
}
}
});
UpDate
when you need to delete all no need to check other option.Just have to call delete
delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
db.open();
db.deleteAll();
db.close();
System.out.println("Record Delete.");
Toast.makeText(getApplicationContext(), "Record Delete.", Toast.LENGTH_SHORT).show();
}
});
Update
public Cursor getAllTitles()
{
Cursor mCursor = db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_ISBN,
KEY_TITLE,
KEY_PUBLISHER},
null,
null,
null,
null,
null);
return mCursor;
}
Update
You have used sqlite auto increment field.So it will be increased forever until it exceeds maximum value.Suppose you have deleted 10 records.If you insert next time the id will be 11.It won't start from 1.See official docs
这篇关于怎么看这个数据库应用程序的删除后插入的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!