移动开发大作业————随手记(数据库和保存到SD卡操作)

保存到SD卡

长按笔记点击保存到SD卡



DBManage.java

package com.cong.notepad;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
public class DBManage {
	private Context mContext = null;
	
	private SQLiteDatabase mSQLiteDatabase = null;//用于操作数据库的对象
	private DBHelper dh = null;//用于创建数据库的对象
	Cursor namecursor=null;
	private String dbName = "notepad.db";
	private int dbVersion = 1;

	public DBManage(Context context){
		mContext = context;
	}
	
	/**
	 * 打开数据库
	 */
	public void open(){
		
		try{
			dh = new DBHelper(mContext, dbName, null, dbVersion);
			if(dh == null){
			
				return ;
			}
			mSQLiteDatabase = dh.getWritableDatabase();
			//dh.onOpen(mSQLiteDatabase);
			Log.i("log", "DB is opened");
		}catch(SQLiteException se){
			se.printStackTrace();
			Log.i("log", "open DB faile");
		}
	}
	
	/**
	 * 关闭数据库
	 */
	public void close(){
		
		mSQLiteDatabase.close();
		dh.close();
		Log.i("log", "DB is closed");
	}
	
	//获取列表
	public Cursor selectAll(){
		Cursor cursor = null;
		try{
			String sql = "select * from travels";
			cursor = mSQLiteDatabase.rawQuery(sql, null);
		}catch(Exception ex){
			ex.printStackTrace();
			cursor = null;
		}
		return cursor;
	}
	
	public Cursor selectById(int id){
		
		//String result[] = {};
		Cursor cursor = null;
		try{
			String sql = "select * from travels where _id='" + id +"'";
			cursor = mSQLiteDatabase.rawQuery(sql, null);
		}catch(Exception ex){
			ex.printStackTrace();
			cursor = null;
		}
		
		return cursor;
	}
	public Cursor selcetPathByName(String name){
		Cursor cursor=null;
		try{
			String sql = "select path from icons where filename='"+ name +"'";
			cursor=mSQLiteDatabase.rawQuery(sql, null);
			Log.i("log", sql);
		}catch(Exception ex){
			ex.printStackTrace();
			Log.i("log", "select faile");
		}
		return cursor;
	}
	//插入数据
	public long insert(String title, String content){
		Log.i("log", "readyto insert");
		long datetime = System.currentTimeMillis();
		Log.i("log", "time------>"+datetime);
		long l = -1;
		try{
			ContentValues cv = new ContentValues();
			cv.put("title", title);
			cv.put("content", content);
			cv.put("time", datetime);
			Log.i("log", "data----->"+title+content+datetime);
			l = mSQLiteDatabase.insert("travels", null, cv);
			Log.i("log", cv.toString());
			Log.i("log", datetime+""+l);
		}catch(Exception ex){
			ex.printStackTrace();
			l = -1;
		}
		return l;
		
	}
	public long inserticonpath(String filename,String iconpath){
		long l=-1;
		try{
			Log.i("log", "ready to insert icon");
			ContentValues cv = new ContentValues();
			cv.put("filename",filename);
			cv.put("path", iconpath);
			l = mSQLiteDatabase.insert("icons", null, cv);
			Log.i("log", "insert iconname success");
		}catch(Exception ex){
			ex.printStackTrace();
			l = -1;
		}
		return l;
	}
	//删除数据
	public int delete(long id){
		int affect = 0;
		try{
			Log.i("log","try to delete the data in databases");
			affect = mSQLiteDatabase.delete("travels", "_id=?", new String[]{id+""});
			Log.i("log", "delete success");
		}catch(Exception ex){
			ex.printStackTrace();
			affect = -1;
			Log.i("log", "delete faile");
		}
		
		return affect;
	}
	
	//修改数据
	public int update(int id, String title, String content){
		int affect = 0;
		try{
			ContentValues cv = new ContentValues();
			
			cv.put("title", title);
			cv.put("content", content);
			affect = mSQLiteDatabase.update("travels", cv, "_id=?", new String[]{id+""});
		}catch(Exception ex){
			ex.printStackTrace();
			affect = -1;
		}
		return affect;
	}
}

DBHelper.java

package com.cong.notepad;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper{
	private String tablename = "travels";
	private String icontable = "icons";
	private Context mcontext=null;
	private String sql = " create table if not exists "+tablename+
			"(_id integer primary key autoincrement, " +
			"title varchar," +
			"content text," +
			"time varchar)";
			String sql2 = "create table if not exists "+icontable+
			"(_id integer primary key autoincrement, " +
			"filename varchar," +
			"path varchar)";
	public DBHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL(sql);
		db.execSQL(sql2);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}

}


  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
android开发期末大作业(项目源码,任务书,实验大报告,apk文件) 大作业的要求和内容:(包括题目选择范围、技术要求、递交时间、考核方法等) 一、实验项目名称 Android手机应用开发课程大作业 二、实验目的 1.通过本课程设计的实践及其前后的准备与总结,复习、领会、巩固和运用课堂上所学的Android手机应用开发知识。 2.为学生综合应用本专业所学习的多门课程知识(例如,软件工程、数据库、Java语言、Java Web开发等)创造实践机会。为学生提供主动学习、积极探索与大胆创新的机会。 3.掌握Android手机应用设计的方法与技巧。 三、实验内容及要求 1、设计内容 题目、设计内容自拟,工作量适中,要求学生应用课程所学知识,采用JAVA语言和Android手机应用开发技术实现一个完整的系统。 ①完成大作业报告。 ②实现各系统功能,并完成调试运行。 2、主要技术 采用Java语言并不仅限于Java语言实现系统。 开发环境与工具:Android Studio 3.2以上版本; 操作系统:Win7/Win10或其他; 4、设计成果: 材料上交:电子文档(大作业任务书+大作业报告+源代码,电子稿请刻在光盘上)、打印稿(大作业任务书+大作业报告)。 四、成绩评定: 考核标准包括: 1、选题的工作量,难度和新颖程度 2、系统架构设计是否良好,运行过程是否报错 3、界面设计的合理性和美观程度 4、基本功能的实现 分值60 (包括布局、组件、Activity、Intent等使用) 数据存储的使用 分值10 网络功能 分值10 Service、ContentProvider或BroadCastReceiver等的使用 分值10 附加分: 图形图像处理、多媒体处理等 分值10 5、考核方式为面对面答辩,在课程的后两周内集中进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值