频道栏目
首页 > 资讯 > Android > 正文

Android中操作SQLite数据库

14-04-10        来源:[db:作者]  
收藏   我要投稿
 
 
创建工程之后,建立一个包,主要是写SQLite的。
 
image
 
再来补充下背景,在这里,我在first这个包里写了两个类,这是因为我在弄两个activity的切换,不影响本实验,本实验是放在OtherActivity.java里面的进行的。
 
建立一个Sqlite.java的类,继承SQLiteOpenHelper。
 
复制代码
public class Sqlite extends SQLiteOpenHelper{
 
    private static final int VERSION = 1;
    
    public Sqlite(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
        // TODO 自动生成的构造函数存根
    }
    public Sqlite(Context context,String name){
        this(context,name,VERSION);
    }
    public Sqlite(Context context,String name,int version){
        this(context, name,null,version);
    }
 
    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO 自动生成的方法存根
        System.out.println("create a Database");
        //execSQL函数用于执行SQL语句
        arg0.execSQL("create table user(id int,name varchar(20))");
    }
 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO 自动生成的方法存根
        System.out.println("update a Database");
    }
 
}
复制代码
在OtherActivity.java中加入这个包,
 
import com.yuyidong.db.Sqlite;
image
 
这里是布局,前面的TextView和Button(Call)请大家无视掉。
 
复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test"
        android:textSize="30sp"
        
        />
    <Button
        android:id="@+id/button_other"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Call"
        />
    <Button
        android:id="@+id/button_create"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Create"
        />
    <Button
        android:id="@+id/button_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Update"
        />
    <Button
        android:id="@+id/button_insert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Insert"
        />
    <Button
        android:id="@+id/button_update_table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Update_Table"
        />
    <Button
        android:id="@+id/button_query"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Query"
        />
    
</LinearLayout>
复制代码
接下来是申明已经添加到监听器中。
 
复制代码
public class OtherActivity extends Activity{
 
    private TextView text;
    private Button button;
    private Button createButton;
    private Button insertButton;
    private Button updateButton;
    private Button updateRecordButton;
    private Button queryButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO 自动生成的方法存根
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
     
        Intent intenter = getIntent();
        String value = intenter.getStringExtra("hello");
        
        text = (TextView) findViewById(R.id.text);
        text.setText(value);
        button = (Button) findViewById(R.id.button_other);
        buttonListener lisbtn = new buttonListener();
        button.setOnClickListener(lisbtn);
        
        createButton = (Button) findViewById(R.id.button_create);
        buttonListener createbtn = new buttonListener();
        createButton.setOnClickListener(createbtn);
        updateButton = (Button) findViewById(R.id.button_update);
        buttonListener updatebtn = new buttonListener();
        updateButton.setOnClickListener(updatebtn);
        insertButton = (Button) findViewById(R.id.button_insert);
        buttonListener insertbtn = new buttonListener();
        insertButton.setOnClickListener(insertbtn);
        updateRecordButton = (Button) findViewById(R.id.button_update_table);
        buttonListener updatetablebtn = new buttonListener();
        updateRecordButton.setOnClickListener(updatetablebtn);
        queryButton = (Button) findViewById(R.id.button_query);
        buttonListener querybtn = new buttonListener();
        queryButton.setOnClickListener(querybtn);
        
    }
复制代码
请大家继续无视掉Intent、text、button这三个对象。
 
接下来讲一讲Button的监听器里面发生的故事。红色的注释是主要的说明。
 
复制代码
class buttonListener implements OnClickListener
    {
        private Button button_check;
        private int version = 1;;
        @Override
        public void onClick(View v) {
            // TODO 自动生成的方法存根
            
//将View的对象v转换成Button的
            button_check = (Button) v;
           //请无视掉这里,这个是转跳到发短信的Activity的Button的操作
            if(button_check==button)
            {
                Uri uri = Uri.parse("smsto:10086");
                Intent intenter = new Intent(Intent.ACTION_SENDTO,uri);
                intenter.putExtra("sms_body", "Test good!");
                startActivity(intenter);
            }
            //如果是按下的创建数据库的那个Button的话,执行
            else if(button_check == createButton)
            {
              //创建一个Sqlite对象  
                Sqlite dbHelper = new Sqlite(OtherActivity.this,"yyd_test_db");
              //只有调用了Sqlite对象的getReadableDatabase()方法,或者是getWritableDatabase()方法之后,才会创建,或打开一个数据库
                SQLiteDatabase db = dbHelper.getReadableDatabase();
               //Toast显示调试
                Toast.makeText(OtherActivity.this, "Create", Toast.LENGTH_SHORT).show();
            }
            else if(button_check == updateButton)
            {
                //每次更新后,数据库版本加1
                version++;
                Sqlite dbHelper = new Sqlite(OtherActivity.this,"yyd_test_db",version);
                SQLiteDatabase db = dbHelper.getReadableDatabase();
                Toast.makeText(OtherActivity.this, "Update", Toast.LENGTH_SHORT).show();
            }
            else if(button_check == insertButton)
            {
                //生成ContentValues对象  
                ContentValues values = new ContentValues();
                //想该对象当中插入键值对,其中键是列名,值是希望插入到这一列的值,值必须和数据库当中的数据类型一致
                values.put("id", 1);
                values.put("name","zhangsan");
                Sqlite dbHelper = new Sqlite(OtherActivity.this, "yyd_test_db", version);
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                //调用insert方法,就可以将数据插入到数据库当中
                db.insert("user", null, values);
                Toast.makeText(OtherActivity.this, "Insert", Toast.LENGTH_SHORT).show();
            }
            else if(button_check == updateRecordButton)
            {
                //得到一个可写的SQLiteDatabase对象
                Sqlite dbHelper = new Sqlite(OtherActivity.this, "yyd_test_db", version);
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put("name", "zhangsanfeng");
             //第一个参数是要更新的表名、第二个参数是一个ContentValeus对象、第三个参数是where子句                
                db.update("user", values, "id=?", new String[]{"1"});
                Toast.makeText(OtherActivity.this, "Update_Table", Toast.LENGTH_SHORT).show();
            }
            else if(button_check == queryButton)
            {
                Sqlite dbHelper = new Sqlite(OtherActivity.this,"yyd_test_db");
                SQLiteDatabase db = dbHelper.getReadableDatabase();
                Cursor cursor = db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);
 
            //用cursor.moveToNext()判断是否还存在下一个,若存在返回真,不存在返回假。
                while(cursor.moveToNext())
                {
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                    System.out.println("Get--->" + name);                    
                }
            }
        }
        
    }
 
复制代码
不仅可以写程序操作SQLite,还可以用shell操作SQLite数据库。
相关TAG标签
上一篇:ADO.NET- 基础总结及实例介绍
下一篇:全球网站现重大安全漏洞:可致银行密码泄露
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站