zoukankan      html  css  js  c++  java
  • SQLite 初步测试

    package org.prothro;
    
    import android.app.Activity;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class SQLiteTestActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
             final SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() + "/db.db3", null);
             
             Button btn = (Button)findViewById(R.id.button1);
             btn.setOnClickListener(new OnClickListener() {
                final    EditText username = (EditText)findViewById(R.id.username);
                final    EditText password = (EditText)findViewById(R.id.password);
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    
                    try{
                        
                        db.execSQL("insert into user values('"+username.getText().toString()+"','"+password.getText().toString()+"')");
                        
                    }catch(SQLException e){
                        e.printStackTrace();
                        db.execSQL(
                                "create table user(username varchar(20),password varchar(20));"
                                );
                        db.execSQL("insert into user values('"+username+"','"+password+"')");
                    }
                    
                    //取出数据库中所有的数据
                    Cursor cursor = db.rawQuery("select * from user", null);
                    
                    while(cursor.moveToNext()){
                        
                        Toast.makeText(
                                SQLiteTestActivity.this, 
                                cursor.getString(cursor.getColumnIndex("username"))+"   "+cursor.getString(cursor.getColumnIndex("password")), 
                                1000).show();
                    }
                    
                }
            });
        }
    }
  • 相关阅读:
    帧同步资料收集
    随机数种子问题
    【转】 DOTA2中的伪随机及其lua实现
    C++ 异常机制分析
    细说new与malloc的10点区别
    static关键字总结
    C++11 并发编程基础(一):并发、并行与C++多线程
    论一个程序员的自我修养
    gSoap的多线程程序
    面试常见问题:
  • 原文地址:https://www.cnblogs.com/laoquans/p/3070682.html
Copyright © 2011-2022 走看看