// TODO Auto-generated constructor stub
}
/**
* 第一没有数据库创建的时候执行,以后均不执行 创建数据库、数据表和初始化数据库使用
*/
@Override
public void onCreate ( SQLiteDatabase db)
{
/**
* 创建数据表note
*/
StringBuffer stringBufferSQL = new StringBuffer();
stringBufferSQL.append(" Create TABLE note( ");
stringBufferSQL.append(" [noteId] integer PRIMARY KEY AUTOINCREMENT ");
stringBufferSQL.append(" ,[noteTitle] nvarchar(500) ");
stringBufferSQL.append(" ,[noteTime] datetime ");
stringBufferSQL.append(" ,[noteInfo] ntext ");
stringBufferSQL.append(" ) ");
/**
* 执行SQL语句
*/
db.execSQL(stringBufferSQL.toString());
}
/**
* 当数据库版本变更的时候执行 数据库变更的时候使用
*/
@Override
public void onUpgrade ( SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
}
}
/**
* 从数据库中查询到所有的便签
*/
private ArrayList<MyNote> selectByAllNote ()
{
Log.i("lhy", "NoteWorkMainAct:onCreate从数据库中查询所有的便签到主界面显示---");
SQLiteDatabase sqLiteDatabase = createNoteDB.getReadableDatabase();
// 开启事务
sqLiteDatabase.beginTransaction();
notes.clear();
try
{
// 查询所有便签itme数据
Cursor cursor = sqLiteDatabase.query(false, "note", new String[]
{ "noteId", "noteTitle", "noteTime", "noteInfo" }, null, null, null, null, "noteId desc", null);
while (cursor.moveToNext())
{
// 查询到数据后
// 获取便签数据给便签对象
MyNote mynote = new MyNote();
mynote.noteId = cursor.getInt(cursor.getColumnIndex("noteId"));
mynote.noteTitle = cursor.getString(cursor.getColumnIndex("noteTitle"));
mynote.noteTime = new SimpleDateFormat("yyyy-MM-dd hh:mm").parse(cursor.getString(cursor.getColumnIndex("noteTime")));
mynote.noteInfo = cursor.getString(cursor.getColumnIndex("noteInfo"));
// 添加到便签集合
notes.add(mynote);
}
// 提交事务
sqLiteDatabase.setTransactionSuccessful();
// 关闭游标
cursor.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
// 关闭事务
sqLiteDatabase.endTransaction();
sqLiteDatabase.close();
}
return mynotes;
}
Log.i(, mynote);
Log.i("lhy", "NoteWorkMainAct:noteAdd添加新便签到列表集合成功!!");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
sqLiteDatabase.endTransaction();
sqLiteDatabase.close();
}
}
/**
* 便签修改
*/
public void noteEdit ( MyNote mynote)
{
SQLiteDatabase sqLiteDatabase = createNoteDB.getReadableDatabase();
sqLiteDatabase.beginTransaction();
try
{
Log.i("lhy", "NoteWorkMainAct:noteEdit进入修改方法,操作数据库");
ContentValues values = new ContentValues();
// 放弃掉了noteTitle,所以这里永远为空
values.put("noteTitle", "");
values.put("noteInfo", mynote.noteInfo);
sqLiteDatabase.update("note", values, "noteId=", new String[]
{ mynote.noteId + "" });
// 提交事务
sqLiteDatabase.setTransactionSuccessful();
Log.i("lhy", "NoteWorkMainAct:noteEdit修改便签到数据库成功!!");
// 替换集合
Log.i("lhy", "NoteWorkMainAct:noteEdit【替换记录好的待修改便签对象在列表集合下标updateIndex的便签对象】");
Log.i("lhy", "NoteWorkMainAct:noteEdit修改便签到列表集合成功!!");
notes.set(updateIndex, mynote);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
sqLiteDatabase.endTransaction();
sqLiteDatabase.close();
}
}
{
if (notes.get(i).isCheck)
{
// 利用对象ID删除数据库数据
sqLiteDatabase.delete("note", "noteId=", new String[]
{ String.valueOf(notes.get(i).noteId) });
}
}
// 提交事务
sqLiteDatabase.setTransactionSuccessful();
Log.i("lhy", "NoteWorkMainAct:noteDel数据库数据删除成功!!");
Log.i("lhy", "NoteWorkMainAct:noteDel,Iterator迭代出在列表集合中选中了的便签对象,直接删除该对象");
// 删除集合
Iterator<MyNote> iterator = notes.iterator();
while (iterator.hasNext())
{
if (iterator.next().isCheck)
{
iterator.remove();
}
}
Log.i("lhy", "NoteWorkMainAct:noteDel集合数据删除成功!!");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
sqLiteDatabase.endTransaction();
sqLiteDatabase.close();
}
}