android sqlite 问题

yeelone 2011-05-05 12:16:30
以下是我的代码 ,这是我要做的小项目的两个类。重在数据库的操作。
package yeelone.sqlite;

…………包名

public class mySQLite extends ListActivity {

private static int NUM=0;
private static int version=2;
private ImageButton AddList=null;

List<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AddList=(ImageButton)findViewById(R.id.addlist);

AddList.setOnClickListener(new addOnClickListener());
}
//当点击右上角的按钮时,弹出一个dialog,有一个输入框 可以输入内容。内容就是listview要显示的内容,这些内容
是一些标题 。
class addOnClickListener implements Button.OnClickListener{

@Override
public void onClick(View v) {

//创建名为Diary_db的数据库
MySQLhelper dbhelper=new MySQLhelper(mySQLite.this, "Diary_db");
SQLiteDatabase db=dbhelper.getReadableDatabase();
Intent intent=new Intent();
intent.setClass(mySQLite.this,addTitledialog.class);
mySQLite.this.startActivityForResult(intent, 1);

}

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

switch(resultCode)
{
//当弹出的dialog返回时,处理返回的输入的数据
case 1:
//Storage是我自己写的一个类,用于保存listview 的ID信息,还有内容变量
String sid=String.valueOf(Storage.getId());
String str=Storage.getTitle().toString();
String cont=Storage.getContent().toString();


System.out.println("id:"+ sid+" title:"+ str+" content:"+cont);
//设置listview中的内容,内容是标题。
setTitle(str);
MySQLhelper dbhelper=new MySQLhelper(mySQLite.this, "Diary_db",2);
SQLiteDatabase db=dbhelper.getWritableDatabase();
System.out.println("first create dbhelper");
ContentValues values=new ContentValues();
values.put("_id", sid);
values.put("title", str);
values.put("content", cont);
//将数据保存进Diary_db数据库Diary表中
db.insert("Diary", null, values);


break;
case 2:
update();
break;

}
}

private void setTitle(String str){
HashMap<String, String> map=new HashMap<String, String>();

map.put("title",str);
list.add(map);
SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.list_item,new String[]{"title"},new int[]{R.id.list_item});
setListAdapter(adapter);
}

…………………………………………

}

}


package yeelone.sqlopenhelper;

import ……………………

public class MySQLhelper extends SQLiteOpenHelper {

private static final int VERSION=1;
public MySQLhelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);

}
public MySQLhelper(Context context, String name) {
this(context, name,VERSION);


}
public MySQLhelper(Context context, String name,int version) {
this(context, name, null, version);


}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL("create table Diary(_id int primary key ,title varchar(255) not null, content text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("drop table Diary");
onCreate(db);

}
}


我意想中效果应该listview中的内容是可以保存在sqlite中的,所以,我创建一个数据库。
本来应该是点击一次按钮,可以往 数据库里insert一条记录,但是我发现,表里总是只有我最后输入的记录,其它都不见,似乎总是被后一条记录覆盖前一条记录。

我不知道为什么会这样?
另外,我用adb shell进入到/data/data/yeelone.sqlite中,用sqlite3进入数据库
#.schame
却发现没有任何显示,
但是我用eclipse的FILE BROWSER,将数据库导出来,用GUI软件来查看数据库内容,却发现在Diary这张表。


我现在很困惑,从昨晚就一直在搜索如何解决,但是没有办法。



希望大家帮帮忙。

...全文
79 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
呵呵林 2011-05-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lizhengjun2010 的回复:]
MySQLhelper dbhelper=new MySQLhelper(mySQLite.this, "Diary_db",2);

系统将回调函数:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("drop table Diary");
on……
[/Quote]

正解 将2改为1 就行了
或者你可以写个只有一个参数Context的构造函数MySQLhelper(Context ctx)
在里面直接写 super(context, DATABASE_NAME, null, DATABASE_VERSION);
DATABASE_NAME和DATABASE_VERSION 是常量
ps 可以根据自己需要多写几个构造函数 这样用起来会很方便的
lizhengjun2010 2011-05-05
  • 打赏
  • 举报
回复
MySQLhelper dbhelper=new MySQLhelper(mySQLite.this, "Diary_db",2);

系统将回调函数:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("drop table Diary");
onCreate(db);

}
而在这个函数里,你把以前的表给删除了,再重新创建的!所以数据只有当前你插入的一条数据啦!
sibylslove 2011-05-05
  • 打赏
  • 举报
回复
为什么MySQLhelper(mySQLite.this, "Diary_db",2)要换版本啊~
sibylslove 2011-05-05
  • 打赏
  • 举报
回复
你ID都唯一么 我意思是会不会重复~你把你要写在数据库里的信息都打出来看下咧

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧