Android:sqlite:删除记录
我设法找到了Android开发团队未能提供的好例子。但是,我想询问删除是仅在主要活动中还是仅在DBHELPER中使用?我不确定dbhelper.java是否可能被称为dbadapter,因为一些示例似乎显示。我所指的特定语法是您可以使用Internet搜索找到的。但是,最近几个月 /几年中发现的许多示例没有很多,我不是专家:
db.rawquery(“从userDetails中select * name =?”,new String [] []
I managed to find good examples where the android development team failed to provide. However, I wish to ask if the delete can be used only in Main Activity or only in the DBHelper? I'm unsure if DBHelper.java might be called DBAdapter as some examples seem to show. The specific syntax I'm referring to is this which you can certainly find using an internet search. However, not many recent examples are available and many I found in the past months / years have too many files and I'm not an expert:
DB.rawQuery("Select * from Userdetails where name = ?", new String[]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这两个都可以在这两个方面进行。
dbhelper's (dbadapter或您想称之为的任何东西)作业主要是为了提供控制数据库的手段,如果不存在,则创建它(在这种情况下调用了OnCreate 方法),还针对编码版本(调用Overdide
Onupgrade
方法,或在文件的标题中存储),或在更晦涩的环绕OnDewnGrade
方法)。从dbhelper中,您可以通过
getWritabledatabase
或getReadableDatabase
方法获得SQLitedAtabase实例。您用来插入/删除/update/query的SQLitedAtabase对象。SQLitedAtabase提供了采取访问操作的各种方法。例如execsql,RawQuery和许多便利方法,例如插入,更新,删除和查询。
便利方法可以简化问题,通常返回合适的可用值(例如,便利性插入方法返回一个长长的值,这是插入行的rowID的值,查询返回光标对象,即输出值)。
因此,您需要的只是一个sqlitedatabase对象。
示例
也许考虑以下示例,该示例通过dbhelper和mainactivty delete的行(尽管没有删除)。
数据库助手 ahelperforthedatabase
: -
和 MainAttivity ,其中包括4种不同的方式(请参阅注释)删除行(现在再次删除行,因为不存在): -
?
,由值代替的, sqlite参数绑定。这被认为是 safe 为什么它可以保护。Neither, it could be undertaken in either and even both.
The DBHelper's (DBAdapter or whatever you wish to call it) job is primarily to provide a means of controlled opening of the database, creating it if it doesn't exist (in which case the overidden
onCreate
method is called) and also checking the user_version (stored in the file's header) against a coded version (calling the overiddenonUpgrade
method, or in more obscure circumsyances theonDowngrade
method).From the DBHelper you can get an SQLiteDatabase instance via the
getWritableDatabase
or thegetReadableDatabase
methods. It is this SQliteDatabase object that you use to insert/delete/update/query.The SQLitedatabase provides various methods of undertaking the access actions. Such as execSQL, rawQuery and many convenience methods such as insert, update, delete and query.
The convenience methods can simplify matters, often returning suitable usable values (e.g. the convenience insert method returns a long which is the value of the rowid of the inserted row, query returns a Cursor object which is the output values).
So all you require is an SQLiteDatabase object.
Example
Perhaps consider the following example, that DELETE's rows (albeit that there are none to delete) via both the DBHelper and via the MainActivty.
The Database Helper AHelperForTheDatabase
:-
And MainActivity which includes 4 different ways (see comments) to delete rows (again now rows are actually delete as none exist) :-
?
, which is replaced by the value, by SQLite parameter binding. This is considered the safe why as it protects against SQL Injection.