我开始了一个新项目。在这里,我想将数据保存到我的数据库,但由于某种原因,我总是得到这个例外。希望你能帮助我。 也许知道所有这些都在片段中运行是很重要的。
04-04 20:06:39.535 20756-20756/de.company.packageE/SQLiteLog﹕ (1) no such table: trailinfo
04-04 20:06:39.625 20756-20756/de.company.packageE/SQLiteDatabase﹕ Error inserting startzeit=1428170497482 ort_lat=47.8563527 ort_lng=11.8006212
android.database.sqlite.SQLiteException: no such table: trailinfo (code 1): , while compiling: INSERT INTO trailinfo(startzeit,ort_lat,ort_lng) VALUES (?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:892)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:503)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:726)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1568)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1440)
at de.work4dogs.TheMantrailingApp.RecordTrailFragment.trackingStart(RecordTrailFragment.java:213)
at de.work4dogs.TheMantrailingApp.RecordTrailFragment.onClick(RecordTrailFragment.java:268)
at android.view.View.performClick(View.java:4442)
at android.view.View$PerformClick.run(View.java:18473)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
我的DBHelper看起来像这样:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import de.company.package.TrailContract.TrailInfo;
public class TrailInfoDbHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "trail.db";
private static final String SQL_CREATE_TRAILINFO = "CREATE TABLE IF NOT EXISTS " + TrailInfo.TABLE_NAME + " ( " +
TrailInfo._ID + " INTEGER PRIMARY KEY," +
TrailInfo.COLUMN_NAME_TRAILINFO_SUCHHUND + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_AUSLEGEZEIT + " LONG," +
TrailInfo.COLUMN_NAME_TRAILINFO_STARTZEIT + " LONG," +
TrailInfo.COLUMN_NAME_TRAILINFO_ENDZEIT + " LONG," +
TrailInfo.COLUMN_NAME_TRAILINFO_ADRESSE + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_ORT_LAT + " DOUBLE," +
TrailInfo.COLUMN_NAME_TRAILINFO_ORT_LNG + " DOUBLE," +
TrailInfo.COLUMN_NAME_TRAILINFO_RUNNER + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_BEGLEITPERSON + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_TRAILLEGER + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_TRAILLEGER_DETAILS + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_GERUCHSTRAEGER + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_GERUCHSTRAEGER_DETAILS + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_ABGANG + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_UMGEBUNG + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_WITTERUNG + " INTEGER," +
TrailInfo.COLUMN_NAME_TRAILINFO_TEMPERATUR + " INTEGER," +
TrailInfo.COLUMN_NAME_TRAILINFO_BODEN + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_UEBUNGSSCHWERPUNKTE + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_UEBUNGSSCHWERPUNKTE_DETAILS + " TEXT," +
TrailInfo.COLUMN_NAME_TRAILINFO_SCHWERPUNKT_BEWAELTIGT + " INTEGER," +
TrailInfo.COLUMN_NAME_TRAILINFO_MOTIVATION_DES_HUNDES + " REAL," +
TrailInfo.COLUMN_NAME_TRAILINFO_TEAMARBEIT + " REAL," +
TrailInfo.COLUMN_NAME_TRAILINFO_BLIND + " INTEGER," +
TrailInfo.COLUMN_NAME_TRAILINFO_LAENGE + " DOUBLE" +
" )";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + TrailInfo.TABLE_NAME;
public TrailInfoDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_TRAILINFO);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
}
至少在这里我想与数据库进行交互:
TrailInfoDbHelper dbTrailInfoHelper;
private SQLiteDatabase dbTrailInfo; // DB for Trail Info
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
dbTrailInfoHelper = new TrailInfoDbHelper(getActivity());
dbTrailInfo = dbTrailInfoHelper.getWritableDatabase(); // WritableDatabase for the TrailInfo
}
private void trackingStart() {
// Getting start Values for new Trail
long startTime = c.getTime().getTime(); // The Time when the Trail was started
// Write the Data into ContentValue
ContentValues values = new ContentValues();
values.put(TrailInfo.COLUMN_NAME_TRAILINFO_STARTZEIT, startTime);
// Store Data in Database and recieve the TrailID
Id = dbTrailInfo.insert(TrailInfo.TABLE_NAME, null, values);
Toast.makeText(getActivity(), "New ID: " + String.valueOf(trailId), Toast.LENGTH_SHORT).show();
}
定义所有静态的代码
import android.provider.BaseColumns;
public class TrailContract {
public TrailContract() {}
public static abstract class TrailPoints implements BaseColumns {
public static final String TABLE_NAME = "trailpoints";
public static final String COLUMN_NAME_TRAILPOINTS_TRAILID = "trailid";
public static final String COLUMN_NAME_TRAILPOINTS_TIME = "time";
public static final String COLUMN_NAME_TRAILPOINTS_LAT = "lat";
public static final String COLUMN_NAME_TRAILPOINTS_LNG = "lng";
}
public static abstract class TrailInfo implements BaseColumns {
public static final String TABLE_NAME = "trailinfo";
public static final String COLUMN_NAME_TRAILINFO_SUCHHUND = "suchhund";
public static final String COLUMN_NAME_TRAILINFO_AUSLEGEZEIT = "auslegezeit";
public static final String COLUMN_NAME_TRAILINFO_STARTZEIT = "startzeit";
public static final String COLUMN_NAME_TRAILINFO_ENDZEIT = "endzeit";
public static final String COLUMN_NAME_TRAILINFO_ADRESSE = "adresse";
public static final String COLUMN_NAME_TRAILINFO_ORT_LAT = "ort_lat";
public static final String COLUMN_NAME_TRAILINFO_ORT_LNG = "ort_lng";
public static final String COLUMN_NAME_TRAILINFO_RUNNER = "runner";
public static final String COLUMN_NAME_TRAILINFO_BEGLEITPERSON = "begleitperson";
public static final String COLUMN_NAME_TRAILINFO_TRAILLEGER = "trailleger";
public static final String COLUMN_NAME_TRAILINFO_TRAILLEGER_DETAILS = "trailleger_details";
public static final String COLUMN_NAME_TRAILINFO_GERUCHSTRAEGER = "geruchstraeger";
public static final String COLUMN_NAME_TRAILINFO_GERUCHSTRAEGER_DETAILS = "geruchstraeger_details";
public static final String COLUMN_NAME_TRAILINFO_ABGANG = "abgang";
public static final String COLUMN_NAME_TRAILINFO_UMGEBUNG = "umgebung";
public static final String COLUMN_NAME_TRAILINFO_WITTERUNG = "witterung";
public static final String COLUMN_NAME_TRAILINFO_TEMPERATUR = "temperatur";
public static final String COLUMN_NAME_TRAILINFO_BODEN = "boden";
public static final String COLUMN_NAME_TRAILINFO_UEBUNGSSCHWERPUNKTE = "uebungsschwerpunkte";
public static final String COLUMN_NAME_TRAILINFO_UEBUNGSSCHWERPUNKTE_DETAILS = "uebungsschwerpunkte_details";
public static final String COLUMN_NAME_TRAILINFO_SCHWERPUNKT_BEWAELTIGT = "schwerpunkt_bewaeltigt";
public static final String COLUMN_NAME_TRAILINFO_MOTIVATION_DES_HUNDES = "motivation_des_hundes";
public static final String COLUMN_NAME_TRAILINFO_TEAMARBEIT = "teamarbeit";
public static final String COLUMN_NAME_TRAILINFO_BLIND = "blind";
public static final String COLUMN_NAME_TRAILINFO_LAENGE = "laenge";
}
}