@TianCheng 2016-04-22 04:04 采纳率: 100%
浏览 1628
已采纳

关于安卓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"));
    }
}

}

求大神帮忙解答下!谢谢

  • 写回答

6条回答 默认 最新

  • erichk2008 2016-04-22 05:02
    关注

    db = new DBHelper(c).getWritableDatabase();
    可以去DBHelper()类里看一下初始化数据库的代码,一般在onCreate()方法里,是不是没有建表?
    或者建表语言写了,但是忘记调用db.execSQL(String 建表语句)方法。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决