程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 安卓數據庫-關於安卓SQLite的問題

安卓數據庫-關於安卓SQLite的問題

編輯:編程解疑
關於安卓SQLite的問題

小弟在學習的時候,遇到了一些困難,求大神解答!
這是我遇到的困難:
圖片說明
我寫的代碼:
其中一個是AddDAO接口:
package com.AA.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AddDAO {
SQLiteDatabase db;

public AddDAO(Context c){
    db = new DBHelper(c).getWritableDatabase();
}

public void add(ContentValues cv) {
    db.insert(DBConstants2.add.TABLE_NAME, null, cv);
}

public void update(long id, ContentValues cv) {
    db.update(DBConstants2.add.TABLE_NAME, cv, "_id = ?", new String[] { String.valueOf(id) });
}

public void del(long id) {
    db.delete(DBConstants2.add.TABLE_NAME, "_id = ?", new String[] { String.valueOf(id) });
}

public Cursor queryAll() {
    return db.rawQuery("select * from add", null);
}

public Cursor queryAllByType(int type) {
    return db.rawQuery("select * from add where type = ?", new String[] { String.valueOf(type) });
}

public Cursor queryAllDetail(String start, String end) {
    return db.rawQuery("select * from add where [date] >= ? and [date] <= ?", new String[] { start, end });
}

public Cursor queryForTypeAll(String start, String end, int type) {
    return db.rawQuery("select sum(time) from add where [date] >= ? and [date] <= ? and type =?",
            new String[] { start, end, String.valueOf(type) });
}

public Cursor queryAllDay(String date) {
    return db.rawQuery("select * from add where [date] = ? ", new String[] { date });
}

public Cursor queryAllDayByType(String date, int type) {
    return db.rawQuery("select * from add where [date] = ? and type = ?",
            new String[] { date, String.valueOf(type) });
}

public Cursor queryAllForKind(String start, String end, int type) {
    return db.rawQuery("select * from add where [date] >= ? and [date] <= ? and type =? group by kind",
            new String[] { start, end, String.valueOf(type) });
}

}

還有一個是OtherdataDAO2:
package com.AA.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class OtherdataDAO2 {
SQLiteDatabase db;

public OtherdataDAO2(Context c) {
    db = new DBHelper(c).getWritableDatabase();
}

public void add(ContentValues cv) {
    db.insert(DBConstants2.ActionList.TABLE_NAME, null, cv);
}

public void update(long id, ContentValues cv) {
    db.update(DBConstants2.ActionList.TABLE_NAME, cv, "_id = ?", new String[]{String.valueOf(id)});
}

public void del(long id) {
    db.delete(DBConstants2.ActionList.TABLE_NAME, "_id = ?", new String[]{String.valueOf(id)});
}

public Cursor queryAll(int type) {
    return db.rawQuery("select * from ActionList where type = ?", new String[]{String.valueOf(type)});
}

public Cursor queryById(long id) {
    return db.rawQuery("select * from ActionList where _id = ?", new String[]{String.valueOf(id)});
}

public String queryForFirst(int type) {
    Cursor cursor = db.rawQuery("select * from ActionList where type = ? limit 0,1", new String[]{String.valueOf(type)});
    if(cursor.moveToNext()) {
        return cursor.getString(cursor.getColumnIndex(DBConstants2.ActionList.NAME));
    }
    return "其他";
}

}

然後RecordActivity2:
package com.AA.record;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.AA.database.ActionListDAO;
import com.AA.database.AddDAO;
import com.AA.database.DBConstants2;
import com.AA.database.OtherdataDAO2;
import com.AA.f1.R;
import com.AA.otherdata.OtherdataListActivity2;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RecordActivity2 extends Activity {

TextView tv03,tv05,tv07,tv09,tv11,tv13;
EditText et01,et02;
ActionListDAO aldao;
AddDAO adao;
OtherdataDAO2 oddao2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record);
    init();
    initData();
    }

//初始化函數
    private void init() {
        tv03 = (TextView) findViewById(R.id.tv03);//運動類型選項
        tv05 = (TextView) findViewById(R.id.tv05);//鍛煉部位選項
        tv07 = (TextView) findViewById(R.id.tv07);//鍛煉內容選項
        tv09= (TextView) findViewById(R.id.tv09);//項目次數選項
        tv11 = (TextView) findViewById(R.id.tv11);//力量級別選項
        tv13 = (TextView) findViewById(R.id.tv13);//鍛煉日期選項
        et01 = (EditText) findViewById(R.id.et01);//鍛煉時長
        et02 = (EditText) findViewById(R.id.et02);//備注欄
        adao=new AddDAO(this);//用於輸入數據
        aldao=new ActionListDAO(this);//用於選擇動作
        oddao2 = new OtherdataDAO2(this);//用於添加至明細
    }

@SuppressLint("SimpleDateFormat") 
private void initData() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    tv09.setText(sdf.format(new Date()));
    tv03.setText(aldao.queryForFirst(DBConstants2.ActionList.TYPE_SportType));
    tv05.setText(aldao.queryForFirst(DBConstants2.ActionList.TYPE_FitnessPart));
    tv07.setText(aldao.queryForFirst(DBConstants2.ActionList.TYPE_FitnessContent));
    tv11.setText(aldao.queryForFirst(DBConstants2.ActionList.TYPE_StrengthLevel));
    tv13.setText(aldao.queryForFirst(DBConstants2.ActionList.TYPE_ProjectNumber));
}

public void toDate(View v) {
    Calendar c = Calendar.getInstance();
    DatePickerDialog dialog = new DatePickerDialog(this, new OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            tv09.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
        }
    }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
    dialog.show();
}

public void back(View v) {
    finish();
}

public void add(View v) {
    ContentValues cv = new ContentValues();
    cv.put(DBConstants2.add.TIME, et01.getText().toString());
    cv.put(DBConstants2.add.SportType, tv03.getText().toString());
    cv.put(DBConstants2.add.FitnessPart, tv05.getText().toString());
    cv.put(DBConstants2.add.FitnessContent, tv07.getText().toString());
    cv.put(DBConstants2.add.DATE, tv09.getText().toString());
    cv.put(DBConstants2.add.StrengthLevel, tv11.getText().toString());
    cv.put(DBConstants2.add.ProjectNumber, tv13.getText().toString());
    cv.put(DBConstants2.add.MARK, et02.getText().toString());
    //cv.put(DBConstants2.inout.MONEY, et01.getText().toString());
    //cv.put(DBConstants2.inout.KIND, tv03.getText().toString());
    //cv.put(DBConstants2.inout.ACCOUNT, tv05.getText().toString());
    //cv.put(DBConstants2.inout.SHOP, tv07.getText().toString());
    ///cv.put(DBConstants2.inout.DATE, tv09.getText().toString());
    //cv.put(DBConstants2.inout.MEMBER, tv11.getText().toString());
    //cv.put(DBConstants2.inout.PROJECT, tv13.getText().toString());
    //cv.put(DBConstants2.inout.MARK, et02.getText().toString());
    //cv.put(DBConstants.inout.TYPE, DBConstants.inout.TYPE_IN);
    adao.add(cv);
    Toast.makeText(this, "記錄成功!!!", Toast.LENGTH_SHORT).show();
    finish();
}

//從Otherdatalist裡獲取動作列表
public void toOther(View v) {
    Intent i = new Intent(this, OtherdataListActivity2.class);
    i.putExtra("rs", true);
    int requestCode = 0;
    switch (v.getId()) {
    case R.id.tv03:
        requestCode = 3;
        i.putExtra("type", DBConstants2.ActionList.TYPE_SportType);
        break;
    case R.id.tv05:
        requestCode = 5;
        i.putExtra("type", DBConstants2.ActionList.TYPE_FitnessPart);
        break;
    case R.id.tv07:
        requestCode = 7;
        i.putExtra("type", DBConstants2.ActionList.TYPE_FitnessContent);
        break;
    case R.id.tv11:
        requestCode = 11;
        i.putExtra("type", DBConstants2.ActionList.TYPE_StrengthLevel);
        break;
    case R.id.tv13:
        requestCode = 13;
        i.putExtra("type", DBConstants2.ActionList.TYPE_ProjectNumber);
        break;
    }
    startActivityForResult(i, requestCode);
}

//進入OtherdataList裡可以進行選擇動作
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 3 && resultCode == RESULT_OK) {
        tv03.setText(data.getStringExtra("value"));
    }else if (requestCode == 5 &&resultCode == RESULT_OK){
        tv05.setText(data.getStringExtra("value"));
    }else if (requestCode == 7 && resultCode == RESULT_OK) {
        tv07.setText(data.getStringExtra("value"));
    } else if (requestCode == 11 && resultCode == RESULT_OK) {
        tv11.setText(data.getStringExtra("value"));
    } else if (requestCode == 13 && resultCode == RESULT_OK) {
        tv13.setText(data.getStringExtra("value"));
    }
}

}

求大神幫忙解答下!謝謝

最佳回答:


db = new DBHelper(c).getWritableDatabase();
可以去DBHelper()類裡看一下初始化數據庫的代碼,一般在onCreate()方法裡,是不是沒有建表?
或者建表語言寫了,但是忘記調用db.execSQL(String 建表語句)方法。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved