Android Studio SQLite3创建数据库

时间:2015-03-16 03:47:18

标签: android sqlite

我试图在Android Studio中创建一个应用程序,用于记录gps位置,并使用SQLite作为数据库。我是java / android工作室的初学者,并参加过关于如何通过youtube上的vids创建项目的速成课程。

我已将create database代码放在一个函数上,并在create上调用。我能够建立没有错误的程序,但我不断得到“数据库缺失”吐司。我错过了什么吗?

#MainActivity.java

import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;

public class MainActivity extends ActionBarActivity {

SQLiteDatabase recordedDB = null;
GPSLocation gps;
int ReadingCount = 0;
float oldAcc,newAcc;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ReAcquireData();
    CreateDB();




}



public void Save(View view) {

}

public void ReAcquireData(View view) {
    ReAcquireData();

}

public void ReAcquireData(){
    final TextView tvLat = (TextView) findViewById(R.id.tvLat);
    final TextView tvLong = (TextView) findViewById(R.id.tvLong);
    final TextView tvAcc = (TextView) findViewById(R.id.tvAcc);
    final TextView tvRead = (TextView) findViewById(R.id.tvReadings);

    gps = new GPSLocation(MainActivity.this);
    if (gps.canGetLocation()) {
        ReadingCount++;

        if (ReadingCount == 1) {
            oldAcc = gps.getAccuracy();
            tvLat.setText(Double.toString(gps.getLatitude()));
            tvLong.setText(Double.toString(gps.getLongitude()));
            tvAcc.setText(Float.toString(oldAcc) + "m");
        }
        else {
            newAcc = gps.getAccuracy();
            if (newAcc < oldAcc) {
                oldAcc = newAcc;
                tvLat.setText(Double.toString(gps.getLatitude()));
                tvLong.setText(Double.toString(gps.getLongitude()));
                tvAcc.setText(Float.toString(oldAcc) + "m");
            }
        }
        tvRead.setText(Integer.toString(ReadingCount));
    } else gps.showSettingsAlert();
}

public void CreateDB(){
    try{
        recordedDB = this.openOrCreateDatabase("GeoLocator",MODE_PRIVATE,null);
        recordedDB.execSQL("CREATE TABLE IF NOT EXISTS database ("+
                        "entry integer primary key,"+
                        "operatorID VARCHAR,"+
                        "municipality VARCHAR,"+
                        "barangay VARCHAR,"+
                        "longitude DOUBLE,"+
                        "latitude DOUBLE,"+
                        "accuracy FLOAT,"+
                        "landcover VARCHAR,"+
                        "remarks VARCHAR);"
        );

        File database = getApplicationContext().getDatabasePath("GeoLocator.db");

        if(database.exists())
            Toast.makeText(this,"Database Exists", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this,"Database Missing", Toast.LENGTH_SHORT).show();

    } catch(Exception e){
        Log.e("GeoLocator ERROR","Error Creating Database");
    }

}
}

1 个答案:

答案 0 :(得分:0)

只需确保将用于创建数据库的相同字符串名称传递给getDatbasePath()方法。

替换

File database = getApplicationContext().getDatabasePath("GeoLocator.db");

使用

File database = getApplicationContext().getDatabasePath("GeoLocator");