你的東東d以創建數據庫文件,如下所示:
public class SQLiteDataBaseAdapter extends Activity {
SQLiteHelper helper;
SQLiteDatabase db, db1;
long id, id1, id2;
public SQLiteDataBaseAdapter(Context context) {
helper = new SQLiteHelper(context);
}
// Inserting Data into the DaTaBase
public long insertData(String task_name, String contact_name, String contact_number, String description, String remarks,
String date, String time, String est_comp_date, String est_comp_time, String act_comp_date, String act_comp_time, String notify_date, String notify_time) {
db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(SQLiteHelper.TASK_NAME, task_name);
contentValues.put(SQLiteHelper.CONTACT_NAME, contact_name);
contentValues.put(SQLiteHelper.CONTACT_NUMBER, contact_number);
//contentValues.put(SQLiteHelper.CONTACT_EMAIL, contact_email);
contentValues.put(SQLiteHelper.DESCRIPTION, description);
contentValues.put(SQLiteHelper.REMARKS, remarks);
contentValues.put(SQLiteHelper.DATE, date);
contentValues.put(SQLiteHelper.TIME, time);
contentValues.put(SQLiteHelper.ESTIMATED_COMPLETION_DATE, est_comp_date);
contentValues.put(SQLiteHelper.ESTIMATED_COMPLETION_TIME, est_comp_time);
contentValues.put(SQLiteHelper.ACTUAL_COMPLETION_DATE, act_comp_date);
contentValues.put(SQLiteHelper.ACTUAL_COMPLETION_TIME, act_comp_time);
contentValues.put(SQLiteHelper.NOTIFY_DATE, notify_date);
contentValues.put(SQLiteHelper.NOTIFY_TIME, notify_time);
id = db.insert(SQLiteHelper.TABLE_NAME, null, contentValues);
Log.d("PaNa", "The value of Id is " + id);
db.close();
return id;
}
public Cursor getAllData() {
db = helper.getWritableDatabase();
String[] columns = {SQLiteHelper.UID,
SQLiteHelper.TASK_NAME,
SQLiteHelper.CONTACT_NAME,
SQLiteHelper.CONTACT_NUMBER,
//SQLiteHelper.CONTACT_EMAIL,
SQLiteHelper.DESCRIPTION,
SQLiteHelper.REMARKS,
SQLiteHelper.DATE,
SQLiteHelper.TIME,
SQLiteHelper.ESTIMATED_COMPLETION_DATE,
SQLiteHelper.ESTIMATED_COMPLETION_TIME,
SQLiteHelper.ACTUAL_COMPLETION_DATE,
SQLiteHelper.ACTUAL_COMPLETION_TIME,
SQLiteHelper.NOTIFY_DATE,
SQLiteHelper.NOTIFY_TIME};
Cursor cursor = db.query(SQLiteHelper.TABLE_NAME, columns, null, null,
null, null, null);
StringBuffer buffer = new StringBuffer();
while (cursor.moveToNext()) {
int cid = cursor.getInt(0);
String taskName = cursor.getString(1);
String est_comp_time = cursor.getString(6);
buffer.append(cid + " " + taskName + " " + est_comp_time + "\n");
}
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
//Obtain a single row
public Task getTask(String id) {
Log.d("Pana", "The value of id is " + String.valueOf(id));
db = helper.getReadableDatabase();
String[] columns = {SQLiteHelper.UID,
SQLiteHelper.TASK_NAME, SQLiteHelper.CONTACT_NAME, SQLiteHelper.CONTACT_NUMBER, SQLiteHelper.DESCRIPTION, SQLiteHelper.REMARKS, SQLiteHelper.DATE, SQLiteHelper.TIME, SQLiteHelper.ESTIMATED_COMPLETION_DATE, SQLiteHelper.ESTIMATED_COMPLETION_TIME,
SQLiteHelper.ACTUAL_COMPLETION_DATE, SQLiteHelper.ACTUAL_COMPLETION_TIME, SQLiteHelper.NOTIFY_DATE, SQLiteHelper.NOTIFY_TIME};
Cursor cursor = db.query(SQLiteHelper.TABLE_NAME, columns, SQLiteHelper.UID + "= ?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Task task = new Task(cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5),
cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getString(10),
cursor.getString(11), cursor.getString(12), cursor.getString(13));
cursor.close();
return task;
}
}
class SQLiteHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "task_management_database";
public static final String TABLE_NAME = "DataTable";
public static final String UID = "_id";
public static final String TASK_NAME = "Task_name";
public static final String CONTACT_NAME = "Contact_Name";
public static final String CONTACT_NUMBER = "Contact_Number";
public static final String CONTACT_EMAIL = "Contact_Email";
public static final String DESCRIPTION = "Description";
public static final String REMARKS = "Remarks";
public static final String DATE = "Date";
public static final String TIME = "Time";
public static final String ESTIMATED_COMPLETION_DATE = "EstCompDate";
public static final String ESTIMATED_COMPLETION_TIME = "EstCompTime";
public static final String ACTUAL_COMPLETION_DATE = "ActCompDate";
public static final String ACTUAL_COMPLETION_TIME = "ActCompTime";
public static final String NOTIFY_DATE = "NotifyDate";
public static final String NOTIFY_TIME = "NotifyTime"
public static final int DATABASE_VERSION = 1;
public static final String SUB_TASK_NUMBER = "sub_task_number";
public static final String CREATE_TABLE = " CREATE TABLE " + TABLE_NAME +
"(" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TASK_NAME + " VARCHAR(250) UNIQUE," + CONTACT_NAME + " VARCHAR(250)," + CONTACT_NUMBER + " VARCHAR(250),"
+ CONTACT_EMAIL + " VARCHAR(250)," + DESCRIPTION + " VARCHAR(250), " + REMARKS + " VARCHAR(250),"
+ DATE + " VARCHAR(250)," + TIME + " VARCHAR(250)," + ESTIMATED_COMPLETION_DATE + " VARCHAR(250), " + ESTIMATED_COMPLETION_TIME + " VARCHAR(250), "
+ ACTUAL_COMPLETION_DATE + " VARCHAR(250), " + ACTUAL_COMPLETION_TIME + " VARCHAR(250), " + NOTIFY_DATE + " VARCHAR(250), " + NOTIFY_TIME + " VARCHAR(250), ");";
public static final String DROP_TABLE = "DROP TABLE IF EXISTS" + TABLE_NAME;
// private Context context;
public SQLiteHelper(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// This is the method which is executed first
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
try {
db.execSQL(DROP_TABLE);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
onCreate(db);
}
}
我已示於機器人,如何插入數據以及如何讀取數據創建數據庫的樣品的例子。你需要按照你的應用程序進行修改。內部類擴展SQLiteOpenHelper,並從主類訪問屬性。
希望這可以幫助你。
您是否創建了數據庫? – Keshav1234 2015-04-01 08:17:12
是的。我創建了數據庫。我把數據/數據/ com.map /數據庫/ Landslide.sqlite – 2015-04-01 08:37:52
不理解你,你不需要把你的數據庫那裏它自動去那裏,你有擴展SQLiteOpenHelper類嗎? – Keshav1234 2015-04-01 08:44:03