我正在开发一个小应用程序,它将位置纬度和经度存储在SQLite数据库中。我有三个课程如下:
AddPlaceActyivity.java
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.rememberthislocation.R;
public class AddPlaceActivity extends Activity{
Button btn_remember,btn_maps;
Fragment fr;
Button addlocation;
EditText locationname;
String mylocationname,mylatitude,mylongitude;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.d("my","In oncreateview");
setContentView(R.layout.remember_fragment);
addlocation = (Button)findViewById(R.id.btn_addlocation);
locationname = (EditText)findViewById(R.id.locationname);
mylocationname = locationname.getText().toString();
Log.d("my","value 1 taken");
mylatitude = "0.0";
mylongitude = "0.0";
Log.d("my", "data values taken");
addlocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
MySqliteHelper db = new MySqliteHelper(getApplicationContext());
db.addPlaces(new Place(mylocationname,mylatitude,mylongitude));
}
});
}
}
MySqliteHelper.java
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySqliteHelper extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contactsManager";
private static final String KEY_ID = "id";
private static final String KEY_PLACENAME = "placename";
private static final String KEY_LATITUDE = "latitude";
private static final String KEY_LONGITUDE = "longitude";
private static final String TABLE_NAME = "places";
public MySqliteHelper(Context context) {
super(context, DATABASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//actual query = create table places (id primary key autoincrement, placename taxt, latitude real, longitude real);
String query = "CREATE TABLE" +TABLE_NAME + "( " + KEY_ID+
"INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_PLACENAME+
"TEXT, "+KEY_LATITUDE+
"REAL, "+KEY_LONGITUDE+ "REAL)";
try {
db.execSQL(query);
Log.d("my", "Successfully created table: " + query);
} catch (SQLiteException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS places");
this.onCreate(db);
}
public void addPlaces(Place place)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues convalues = new ContentValues();
convalues.put(KEY_PLACENAME,place.getname());
convalues.put(KEY_LATITUDE,place.getlatitude());
convalues.put(KEY_LONGITUDE,place.getlongitude());
db.insert(TABLE_NAME, null, convalues);
Log.d("my","db.insert(TABLE_NAME, null, convalues)");
Log.d("my", "Values inserted");
db.close();
}
}
Place.java
public class Place {
String mname,mlatitude,mlongitude,mstring;
public Place(String name, String latitude, String longitude)
{
mname = name;
mlatitude = latitude;
mlongitude = longitude;
}
public Place() {
// TODO Auto-generated constructor stub
}
public void setname(String placename)
{
mname = placename;
}
public String getname()
{
return mname;
}
public void setlatitude(String latitude)
{
mlatitude=latitude;
}
public String getlatitude()
{
return mlatitude;
}
public void setlongitude(String longitude)
{
mlongitude = longitude;
}
public String getlongitude()
{
return mlongitude;
}
}
我的logcat日志:
11-20 17:40:02.770: D/my(10604): In oncreateview
11-20 17:40:02.890: D/my(10604): acivity layout initialized
11-20 17:40:02.950: I/PGA(10604): New SOCKET connection: berthislocation (pid 10604, tid 10604)
11-20 17:40:16.510: D/my(11196): in remember's onclick
11-20 17:40:16.540: D/my(11196): intent started
11-20 17:40:16.560: D/my(11196): In oncreateview
11-20 17:40:16.560: D/my(11196): value 1 taken
11-20 17:40:16.560: D/my(11196): data values taken
11-20 17:40:27.290: I/SqliteDatabaseCpp(11196): sqlite returned: error code = 1, msg = no such table: places, db=/data/data/com.example.rememberthislocation/databases/contactsManager
11-20 17:40:27.290: E/SQLiteDatabase(11196): Error inserting longitude=0.0 latitude=0.0 placename=
11-20 17:40:27.290: E/SQLiteDatabase(11196): android.database.sqlite.SQLiteException: no such table: places: , while compiling: INSERT INTO places(longitude,latitude,placename) VALUES (?,?,?)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:112)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at com.example.rememberthisplace.MySqliteHelper.addPlaces(MySqliteHelper.java:74)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at com.example.rememberthisplace.AddPlaceActivity$1.onClick(AddPlaceActivity.java:52)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.view.View.performClick(View.java:3511)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.view.View$PerformClick.run(View.java:14105)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.os.Handler.handleCallback(Handler.java:605)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.os.Handler.dispatchMessage(Handler.java:92)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.os.Looper.loop(Looper.java:137)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at java.lang.reflect.Method.invokeNative(Native Method)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at java.lang.reflect.Method.invoke(Method.java:511)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
11-20 17:40:27.290: E/SQLiteDatabase(11196): at dalvik.system.NativeStart.main(Native Method)
显然错误发生在MySqliteHelper.java
中Error: sqlite returned: error code = 1, msg = no such table: places
因此,我的MySqliteHelper类的oncreate()没有被调用。我试了好几个小时..但是找不到解决办法。任何人都可以帮助我吗?
答案 0 :(得分:5)
您的onCreate()
中有catch
隐藏的错误:
String query = "CREATE TABLE" +TABLE_NAME + "( " + KEY_ID+
"INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_PLACENAME+
"TEXT, "+KEY_LATITUDE+
"REAL, "+KEY_LONGITUDE+ "REAL)";
缺少空白,请更改为:
String query = "CREATE TABLE " +TABLE_NAME + "( " + KEY_ID+
" INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_PLACENAME+
" TEXT, "+KEY_LATITUDE+
" REAL, "+KEY_LONGITUDE+ " REAL)";
删除try-catch。如果出现问题,应该抛出框架异常。如果onCreate()
正常返回,则框架认为数据库已成功设置。 onCreate()
仅在首次设置数据库时调用一次。
卸载您的应用或清除其数据以删除旧的空数据库文件并重新运行onCreate()
。
进一步阅读:When is SQLiteOpenHelper onCreate() / onUpgrade() run?