当前位置: 代码迷 >> Android >> Android SQLite基础一部分-新手求教
  详细解决方案

Android SQLite基础一部分-新手求教

热度:105   发布时间:2016-04-28 00:30:51.0
Android SQLite基础部分--新手求教
新手请教,最近有在学习Android SQLite的基础知识,如下源码不知道为什么运行不了,模拟器报错"UNFORTUNATEL,...HAS STOPPED",想要实现用Android链接SQLite的功能,第一次接触Android开发,请帮忙解答,谢谢。。


//数据库类
package com.example.android_beacher;

import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.content.Context;

public class New_DB extends SQLiteOpenHelper {

private static final String DB_NAME = "mydata.db"; //数据库名称
    private static final int version = 1; //数据库版本
private static final MainActivity Context = null;
    
public New_DB(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
arg0.execSQL("create table PATH_INFO_t( PATH_INFO text primary key,CREATR_DATA timestamp not null default (datetime('now','localtime')))");
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}
public void testCreateDB() {
New_DB helper = new New_DB(Context, DB_NAME, null, version);
helper.getWritableDatabase(); // 创建数据库
}


}

//主程序UI类
package com.example.android_beacher;

//import android.R;
import android.R.id;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.widget.LinearLayout; 
import android.widget.Toast;
import android.content.ContentValues;
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class MainActivity extends Activity {

SQLiteDatabase db=null;
public String db_name="mydata.db";
public String tabel_name="PATH_INFO_t";
final New_DB helper=new New_DB(this,db_name,null,1);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.setContentView(R.layout.activity_main);
      LinearLayout layout = (LinearLayout) super.findViewById(R.id.layout);  
      
    //  Button button = new Button(this);
     final EditText editText = new EditText(this);
    // button.setText("确认");  
    // layout.addView(editText);  
    // layout.addView(button);  
    // button.setOnClickListener(new OnClickListener())
      
      db=helper.getWritableDatabase();
      initDatabase(db);
      
    
    
      final Button but=(Button)this.findViewById(R.id.button);//查询按钮
     but.setText("查询");
      Button but_up=(Button)this.findViewById(R.id.button1);//修改按钮
    
      Button but_add=(Button)this.findViewById(R.id.Button01);//添加按钮
    
      Button but_del=(Button)this.findViewById(R.id.Button02);//删除按钮
     final TextView text=new TextView(this);
    
      
      OnClickListener ocl=new OnClickListener(){

@Override
public void onClick(View vi) {
// TODO Auto-generated method stub

ContentValues cv=new ContentValues();

switch(vi.getId()){

case R.id.button://查询功能

String str=editText.getText().toString();
but.setText("查询中");
Cursor cs=db.rawQuery("select PATH_INFO,CREATR_DATA from PATH_INFO_t where PATH_INFO=?",new String[]{str});
if(cs.moveToFirst())
{  for(int i=0;i<cs.getCount();i++){
cs.move(i);//移动到指定记录
        String username = cs.getString(cs.getColumnIndex("PATH_INFO"));
        String password = cs.getString(cs.getColumnIndex("CREATR_DATA"));
        text.setText(username);
        db.close();
    }}else
    {
     text.setText("error");
    }
case R.id.Button01://插入功能
String ins=editText.getText().toString();
db.execSQL("INSERT INTO PATH_INFO_t(PATH_INFO,CREATR_DATA) VALUES(?,datetime('now','localtime'))", new Object[] {ins});
db.close();
}

}};
 but.setOnClickListener(ocl);
 but_up.setOnClickListener(ocl);
 but_add.setOnClickListener(ocl);
 but_del.setOnClickListener(ocl);
    }
    private void initDatabase(SQLiteDatabase db2) {
// TODO Auto-generated method stub

}
@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;
    }
    
}

------解决思路----------------------
报的错误再列出的多点。做好进入到sqlite里面看看,表、数据库有没有创建成功。
------解决思路----------------------
通常这种错误有两种原因:
1.建表的SQL语句有误,导致建表失败;
2.select语句有错
------解决思路----------------------
应该是表里没有那个字段,你看一下表中的字段。
select 语句应该没问题。
------解决思路----------------------



 if(cs.moveToFirst())
                    {  for(int i=0;i<cs.getCount();i++){
                        cs.move(i);//移动到指定记录
                        String username = cs.getString(cs.getColumnIndex("PATH_INFO"));
                        String password = cs.getString(cs.getColumnIndex("CREATR_DATA"));
                        text.setText(username);
                        db.close();
                    }}else
                    {
                        text.setText("error");
                    }


看的不是很仔细,仅供参考
上面的代码,应该是用cs.moveToNext()来进行迭代的吧
还有,数据库没查完,先不要close,

大概可以改成这样试试看

while(cs.moveToNext()){
              String username = cs.getString(cs.getColumnIndex("PATH_INFO"));
              String password = cs.getString(cs.getColumnIndex("CREATR_DATA"));
              text.setText(username);
}



------解决思路----------------------
参考下:http://blog.csdn.net/gao_chun/article/details/21619551
  相关解决方案