2 cairunbo cairunbo 于 2016.04.22 12:04 提问

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

}

求大神帮忙解答下!谢谢

7个回答

erichk2008
erichk2008   2016.04.22 13:02
已采纳

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

cairunbo
cairunbo 多谢朋友你的回答!
一年多之前 回复
CSDNXIAON
CSDNXIAON   2016.04.22 12:11

安卓Sqlite
安卓sqlite之增删改查(一)
安卓sqlite数据库中sql语句的问题
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

a714530833
a714530833   2016.04.22 12:16

你没建表吧,找不到表怎么进行操作

cairunbo
cairunbo 多谢!
一年多之前 回复
Wei_Smile
Wei_Smile   2016.04.22 12:25

图片说明

看这个情况应该是这个表没建吧

Wei_Smile
Wei_Smile 回复cairunbo: 满意的话给个采纳呗
一年多之前 回复
cairunbo
cairunbo 多谢朋友你的回答!
一年多之前 回复
Leo1208
Leo1208   2016.04.22 13:42

no such table 没有这张表

cairunbo
cairunbo 多谢朋友你的回答!
一年多之前 回复
aiynmimi
aiynmimi   2016.04.22 13:46

错误报的很清楚了,在你RecordActivity2这个类中initData()方法,调用了aldao.queryForFirst()方法,而在OtherdataDAO2 类中queryForFirst()方法的具体逻辑: db.rawQuery("select * from ActionList where type = ? limit 0,1", new String[]{String.valueOf(type)}),此时报错no such table:ActionList,所以应该是你没有建ActionList这张表,所以查询没有找到报错!看下你的建表语句就知道了!

cairunbo
cairunbo 多谢朋友你的回答!
一年多之前 回复
qq_27578023
qq_27578023   2016.05.29 10:57

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!