数据库sql的简单操作

YXTS122 2017-03-27 09:14:38
DatabaseHelper.java
package com.selfteaching.learningdatabases;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper{
public DatabaseHelper(Context context)
{
super(context,"myfirst.db",null,2);
}

public void onCreate(SQLiteDatabase db)
{
String DATABASE_CREATE="create table books(_id integer primary key autoincrement,name text,authors text);";
db.execSQL(DATABASE_CREATE);
}

public void onUpgrade(SQLiteDatabase db,int arg1,int arg2)
{
String sql="DROP TABLE IF EXISTS books";
db.execSQL(sql);
this.onCreate(db);
}

}

DBAdapter.java
package com.selfteaching.learningdatabases;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DBAdapter {
Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context=ctx;
DBHelper=new DatabaseHelper(context);
}
public DBAdapter open() throws SQLException
{
db=DBHelper.getWritableDatabase();
return this;
}
public void close()
{
DBHelper.close();
}
public long insertBook(String name,String authors)
{
ContentValues initialValues=new ContentValues();
initialValues.put("name", name);
initialValues.put("authors", authors);
return db.insert("books", null, initialValues);
}
public boolean deleteBook(long rowId)
{
return db.delete("books", "_id="+rowId, null)>0;
}
public Cursor getAllBooks()
{
return db.query("books", new String[]{"_id","name","authors"}, null, null, null, null, null);
}
public Cursor getBook(long rowId) throws SQLException
{
Cursor mCursor=db.query(true, "books", new String[]{"_id","name","authors"}, "_id="+rowId, null, null, null, null, null);
if (mCursor!=null)
{
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateBook(long rowId,String name,String authors)
{
ContentValues args=new ContentValues();
args.put("name", name);
args.put("authors", authors);
return db.update("books", args, "_id="+rowId, null)>0;
}
}

LearningDatabasesActivity.java
package com.selfteaching.learningdatabases;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class LearningDatabasesActivity extends Activity {
DBAdapter db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_learning_databases);
db=new DBAdapter(this);

db.open();
long id=db.insertBook("Learning Android", "Jim");
id=db.insertBook("Learning Python", "Tom");
db.close();

db.open();
Cursor c=db.getAllBooks();
if (c.moveToFirst())
{
do
{
DisplayBook(c);
}
while (c.moveToNext());
}
db.close();

db.open();
c=db.getBook(2);
if (c.moveToFirst())
{
DisplayBook(c);
}
else
{
Toast.makeText(this, "没找到书!", Toast.LENGTH_LONG).show();
}
db.close();

db.open();
if (db.updateBook(1, "Learning Android 2", "Smith"))
{
Toast.makeText(this, "更新成功!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "更新失败!", Toast.LENGTH_LONG).show();
}
db.close();

db.open();
if (db.deleteBook(1))
{
Toast.makeText(this, "删除成功!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "删除失败!", Toast.LENGTH_LONG).show();
}
db.close();
}

public void DisplayBook(Cursor c)
{
Toast.makeText(this, "id:"+c.getString(0)+"\n Name:"+c.getString(1)+"\n Authors:"+c.getString(2), Toast.LENGTH_LONG).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.learning_databases, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

...全文
130 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinahbbt 2017-03-27
  • 打赏
  • 举报
回复

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧