2 m0 37686614 m0_37686614 于 2017.08.28 10:26 提问

下面的logcat大概是SQL数据库的哪里出现问题

图片说明

4个回答

liang_shui_guo
liang_shui_guo   2017.08.28 10:55
已采纳

就是数据库的表名字拼错了Provicne是什么?应该是Province

m0_37686614
m0_37686614 万分感谢 真特么大傻逼我
3 个月之前 回复
m0_37686614
m0_37686614   2017.08.28 10:32

package com.coolweather.app.db;

import java.util.ArrayList;
import java.util.List;

import com.coolweather.app.model.City;
import com.coolweather.app.model.County;
import com.coolweather.app.model.Province;

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

public class CoolWeatherDB {
public static final String DB_NAME="cool_weather";
public static final int VERSION=1;
private static CoolWeatherDB coolWeatherDB;
private SQLiteDatabase db;
private CoolWeatherDB(Context context) {
CoolWeatherOpenHelper dbHelper=new CoolWeatherOpenHelper(context ,DB_NAME,null,VERSION);
db=dbHelper.getWritableDatabase();
}
public synchronized static CoolWeatherDB getInstance(Context context) {
if(coolWeatherDB==null) {
coolWeatherDB=new CoolWeatherDB(context);
}
return coolWeatherDB;
}
public void saveProvince(Province province) {
if(province!=null) {
ContentValues values=new ContentValues();
values.put("province_name", province.getProvinceName());
values.put("province_code",province.getProvinceCode());
db.insert("Provicne",null, values);
}
}
public List loadProvinces(){
List list=new ArrayList();
Cursor cursor=db.query("Province", null, null, null, null, null, null);
if(cursor.moveToFirst()) {
do {
Province province=new Province();
province.setId(cursor.getInt(cursor.getColumnIndex("id")));
province.setProvinceName(cursor.getString(cursor.getColumnIndex("province_name")));
province.setProvinceCode(cursor.getInt(cursor.getColumnIndex("province_code")));
list.add(province);
}while(cursor.moveToNext());
}
if(cursor!=null) {
cursor.close();
}
return list;
}
public void saveCity(City city) {
if(city!=null) {
ContentValues values=new ContentValues();
values.put("city_name",city.getCityName());
values.put("city_code",city.getCityCode());
values.put("province_id", city.getProvinceId());
db.insert("City", null, values);
}
}
public List loadCities(int provinceId){
List list=new ArrayList();
Cursor cursor=db.query("city",null,"province_id=?",new String[] {String.valueOf(provinceId)},null,null, null);
if(cursor.moveToFirst()) {
do {
City city=new City();
city.setId(cursor.getInt(cursor.getColumnIndex("id")));
city.setCityName(cursor.getString(cursor.getColumnIndex("city_name")));
city.setCityCode(cursor.getInt(cursor.getColumnIndex("city_code")));
city.setProvinceId(provinceId);
list.add(city);
}while(cursor.moveToNext());
}
if(cursor!=null) {
cursor.close();
}
return list;
}
public void saveCounty(County county) {
if(county!=null) {
ContentValues values=new ContentValues();
values.put("county_name", county.getCountyName());
values.put("weather_id", county.getWeatherId());
values.put("city_id", county.getCityId());
db.insert("County", null, values);
}
}
public List loadCounties(int cityId){
List list=new ArrayList();
Cursor cursor=db.query("County",null, "city_id=?",new String[] {String.valueOf(cityId)},null, null, null);
if(cursor.moveToFirst()) {
do {
County county=new County();
county.setId(cursor.getInt(cursor.getColumnIndex("id")));
county.setCountyName(cursor.getString(cursor.getColumnIndex("county_name")));
county.setWeatherId(cursor.getString(cursor.getColumnIndex("weather_id")));
county.setCityId(cityId);
list.add(county);
}while(cursor.moveToNext());
}
if(cursor!=null) {
cursor.close();
}
return list;
}
}
封装SQL代码

Small_Mouse0
Small_Mouse0   Ds   Rxr 2017.08.28 10:37

看这个sql应该没问题,,,是不是数据库设置主键了??,,重复插入同一条数据是有问题的

你把报错那句sql,,在命令行下运行一下,,看看是否能够正常插入

a314066646
a314066646   Rxr 2017.08.28 10:39

表名错了吧?图片说明

a314066646
a314066646 province 你的是provicne 确定没错?
3 个月之前 回复
m0_37686614
m0_37686614 不至于吧 我连接服务器网址下载的这些数据
3 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片