Android中操作SQLite数据库

我又回到了安卓的学习当中,忙来忙去终于忙的差不多有时间做自己的事情了,这感觉实在是太棒了!!本来想写android的控件以及他们的监视器的,但是我查了查android的手册,基本上都能查到,但是查有些功能就比较麻烦,比如EditText中的TextWatcher接口,一般查到的都是OnEditorActionListener接口。好了废话不多说,先割了他!!!!

------------------------咯咯---------------------咯咯------------------------------咯咯------------------------------

创建工程之后,建立一个包,主要是写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数据库。

image

在App程序里面创建了数据库之后才有了databases,登进去数据库,还是与一般关系型数据库还是有差别的。

image

 

总结一下,简短不割,SQLite在App中不提倡使用,因为他有时候会无缘无故的报错,因为我在测试的时候就经常发生,第一次把程序烧进去的时候,可以N次创建数据库同哟个数据库不报错,这就不说了,第二次打开的时候,无论点哪个Button都会直接程序崩溃,包括Insert,第一次的时候就不会,而且还可以查到数据表里面的信息,第二次就不行了,query也query不起了。很蛋疼。

 

 

 

转载请注明出处:http://www.cnblogs.com/yydcdut/p/3651977.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值