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

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

创建工程之后,建立一个包,主要是写SQLite的。

再来补充下背景,在这里,我在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;

这里是布局,前面的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数据库。

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

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

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

最新文章

  1. Atitit 软件开发中&#160;瓦哈比派的核心含义以及修行方法以及对我们生活与工作中的指导意义
  2. [转]-用Gradle 构建你的android程序
  3. wordpress学习-plugins-001
  4. iOS 深拷贝和浅拷贝
  5. new失败判断
  6. 转载 SharePoint Foundation和SharePoint Server的区别
  7. Matlab中tic和toc用法
  8. PHP 绘图技术
  9. java中static特殊性和final(static成员直接被访问,this不能用在static方法中,static不可访问非static)
  10. 微信报错 config:fail.Error:invalid signature
  11. k8s创建pod流程
  12. Fastdfs文件服务器搭建
  13. 隐藏响应的server,X-Powered-By
  14. Git、bower 安装
  15. HDU1505 City Game 悬线法
  16. jdk1.6 支持 tls1.2协议 并忽略身份验证
  17. Java实例---flappy-bird实例[最终版]
  18. C# MessageBox 消息对话框
  19. UILabel设置行间距
  20. swing之JDialog

热门文章

  1. 1020 Tree Traversals (25)(25 point(s))
  2. hdu1527下沙小面的(二)
  3. python中的super( test, self).__init__()
  4. 【SPFA】POJ1511-Invitation Cards
  5. SpringBoot+SpringSecurity+Thymeleaf认证失败返回错误信息踩坑记录
  6. FLASK开发简易blog的学习笔记
  7. URAL 1873. GOV Chronicles
  8. HDU 5154 Harry and Magical Computer bfs
  9. centos7安装zookeeper3.4.9集群
  10. maven打包出错: Failed to clean project: Failed to delete