学习android有一段时间了,前几天学习了android内置的数据库SQLite的使用,学习了一下基础的增删改查,现在就总结一下。下面是总结的使用sql语句的的查看,至于不使用失去了的查询,这里不涉及到。
1.先来学习一下理论的知识:
知识点一:SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么。
例如:可以在Integer类型的字段中存放字符串,或者在布尔型字段中存放浮点数,或者在字符型字段中存放日期型值。
知识点二:使用SQLiteDatabase操作SQLite数据库
Android提供了一个名为SQLiteDatabase的类,该类封装了一些操作数据库的API,使用该类可以完成对数据进行添加(Create)、
查询(Retrieve)、更新(Update)和删除(Delete)操作(这些操作简称为CRUD)。对SQLiteDatabase的学习,我们应该重点掌握
execSQL()和rawQuery()方法。 execSQL()方法可以执行insert、delete、update和CREATE TABLE之类有更改行为的SQL语句;
rawQuery()方法用于执行select语句。
知识点三:
三个重要的方法:
execSQL(String sql)
execSQL(String sql, Object[] bindArgs)
rawQuery(String sql, String[] selectionArgs)
实例,学习完以后,做了一个简的单增删改查实例
案例截图:
java代码如下:
package com.example.androidtest;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText edt_username,edt_password;
private Button btn_create,btn_update,btn_delete,btn_query;
private SQLiteDatabase db ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取按钮
btn_create =(Button) findViewById(R.id.btn_create);
btn_update=(Button) findViewById(R.id.btn_update);
btn_delete=(Button) findViewById(R.id.btn_delete);
btn_query=(Button) findViewById(R.id.btn_query);
//获得输入组件
edt_username=(EditText) findViewById(R.id.et_username);
edt_password=(EditText) findViewById(R.id.et_password);
db= SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/sqltest.db3", null);
db.execSQL("create table if not exists users( username varchar(20) ,password varchar(20) ,primary key (username) )");
//添加
btn_create.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//获得用户输入
String username=edt_username.getText().toString();
String password=edt_password.getText().toString();
try {
db.execSQL("insert into users values (?,?)",new String[]{username,password});
Toast.makeText(MainActivity.this, "插入成功",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "插入失败",Toast.LENGTH_SHORT).show();
}
}
});
//修改
btn_update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String username=edt_username.getText().toString();
String password=edt_password.getText().toString();
try {
db.execSQL("update users set password = ? where username = ? ", new String[]{password,username});
Toast.makeText(MainActivity.this, "修改成功",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "修改失败",Toast.LENGTH_SHORT).show();
}
}
});
//删除
btn_delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String username=edt_username.getText().toString();
try {
db.execSQL("delete from users where username = ? ", new String[]{username});
Toast.makeText(MainActivity.this, "删除成功",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "删除失败",Toast.LENGTH_SHORT).show();
}
}
});
//查询
btn_query.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String username=edt_username.getText().toString();
Cursor result = db.rawQuery("select * from users where username = ?", new String[]{username});
String password=null;
while(result.moveToNext()){
password = result.getString(0);
}
if(password==null){
edt_password.setText("无此用户数据");
Toast.makeText(MainActivity.this, "没有该用户",Toast.LENGTH_SHORT).show();
}else{
edt_password.setText(password);
Toast.makeText(MainActivity.this, "查询成功",Toast.LENGTH_SHORT).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.main, menu);
return true;
}
}
布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:" />
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/tv_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加" />
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除" />
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改" />
<Button
android:id="@+id/btn_query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询" />
</LinearLayout>
</LinearLayout>
最后,具体的项目代码可以下面的链接中下载,如果您觉得可以帮到您的话,就点个赞,或者评论下,谢谢!我们共同进步!
下载链接:http://download.csdn.net/detail/xinghuo0007/9557616