开发者社区> 问答> 正文

在新计算机上运行项目或删除数据库文件夹时,无法在Android Studio中创建SQLite数据库

我构建了一个应用程序,其中使用融合位置提供程序获取位置,并基于该位置显示来自SQLiteDatabase的特定位置的报告卡。

最初,数据库工作正常,但是当我在另一台机器上运行相同的android项目时,它并没有在创建数据库。Android在设备文件资源管理器->数据->数据->包名称->数据库中创建数据库文件夹后。我是通过右键单击“数据库”文件夹并单击“上载”来手动加载以下数据库文件的。数据库文件链接:https : //drive.google.com/file/d/197MHiLl8nvFZ5eHStrBR9WfeYvQTtDKm/view?usp=sharing 为了尝试了解问题所在,我还从自己的计算机上的项目中删除了数据库文件夹,现在数据库文件夹也未在我的计算机上创建。

在将其标记为重复项之前,我已经尝试过以下问题的方法,但仍然无法使用。 未创建Android SQLite数据库表未创建 sqlite数据库 https://alvinalexander.com/android/sqliteopenhelper-does-not-call-oncreate-failing-database

请在下面找到我的代码:DatabaseHelper.java

package edu.cpsc6150.co2ut;

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

public class DatabaseHelper extends SQLiteOpenHelper {

    public static String DATABASE_NAME = "states";
    public static final int DATABASE_VERSION = 1;
    public static final String TABLE_NAME = "States_Report";
    public static final String KEY_ID = "id";
    public static final String STATE_NAME = "name";
    public static final String STATE_GRADE = "grade";
    public static final String EXTREME_HEAT = "heat";
    public static final String DROUGHT = "Drought";
    public static final String WILDFIRES = "wildfires";
    public static final String INLAND_FLOODING = "inlandflooding";
    public static final String COASTAL_FLOODING = "coastalflooding";

    public static final String CREATE_TABLE_STUDENTS = "CREATE TABLE "
            + TABLE_NAME + "(" + KEY_ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + STATE_NAME + " TEXT,"
            + STATE_GRADE + " TEXT,"
            + EXTREME_HEAT + " TEXT,"
            + DROUGHT + " TEXT,"
            + WILDFIRES + " TEXT,"
            + INLAND_FLOODING + " TEXT,"
            + COASTAL_FLOODING + " TEXT)";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        //Log.d("table", CREATE_TABLE_STUDENTS);
    }   //end DatabaseHelper constructor

    /**
     * Functionality: Creates tables
     * PreConditions: needs table names and column names
     * PostConditions: Table is created
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_STUDENTS);
    }   //end onCreate method

    /**
     * Functionality: Drop existing table and create new table if table already exists
     * PreConditions: Database name, oldversion number and newVersion number are required to create a new table
     * PostConditions: Old table should be dropped and new table should be created
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS '" + TABLE_NAME + "'");
        onCreate(db);
    }   //end onUpgrade method
    /*
    public long addStudentDetail(String state, String grade, String heat, String drought, String wildfires, String iflooding, String cflooding) {
        SQLiteDatabase db = this.getWritableDatabase();
        // Creating content values
        ContentValues values = new ContentValues();
        values.put(STATE_NAME, state);
        values.put(STATE_GRADE, grade);
        values.put(EXTREME_HEAT, heat);
        values.put(DROUGHT, drought);
        values.put(WILDFIRES, wildfires);
        values.put(INLAND_FLOODING, iflooding);
        values.put(COASTAL_FLOODING, cflooding);
       // insert row in students table
        long insert = db.insert(TABLE_NAME, null, values);

        return insert;
    }*/

    /**
     * Functionality: Get data from database and Update UI
     * PreConditions: requires existing populated database
     * PostConditions: Update reportcard in the UI from the database
     */
    public String[] getData(String state) {
        /*SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from States_Report where name = '"+state+"'", null );
        return res;*/
        String State = state;
        String[] name = new String[7];
        String password;
        //SQLiteDatabase db = getWritableDatabase();
        SQLiteDatabase db = this.getReadableDatabase();
        String[] columns = {DatabaseHelper.STATE_NAME,DatabaseHelper.STATE_GRADE,DatabaseHelper.EXTREME_HEAT,DatabaseHelper.DROUGHT,DatabaseHelper.WILDFIRES,DatabaseHelper.INLAND_FLOODING,DatabaseHelper.COASTAL_FLOODING};
        Cursor cursor =db.query(DatabaseHelper.TABLE_NAME,columns,"name = '"+state+"' ",null,null,null,null);
        StringBuffer buffer= new StringBuffer();
        while (cursor.moveToNext())
        {
            name[0] =cursor.getString(cursor.getColumnIndex(DatabaseHelper.STATE_NAME));
            name[1] =cursor.getString(cursor.getColumnIndex(DatabaseHelper.STATE_GRADE));
            name[2] =cursor.getString(cursor.getColumnIndex(DatabaseHelper.EXTREME_HEAT));
            name[3] =cursor.getString(cursor.getColumnIndex(DatabaseHelper.DROUGHT));
            name[4] =cursor.getString(cursor.getColumnIndex(DatabaseHelper.WILDFIRES));
            name[5] =cursor.getString(cursor.getColumnIndex(DatabaseHelper.INLAND_FLOODING));
            name[6] =cursor.getString(cursor.getColumnIndex(DatabaseHelper.COASTAL_FLOODING));
        }


        return name;
    }   //end getData method
}   //end DatabaseHelper class
ReportCardActivity.java(与该特定功能的MainActivity.java类似)

package edu.cpsc6150.co2ut;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.Looper;
import android.provider.Settings;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.location.SettingsClient;
import com.google.android.gms.tasks.OnCompleteListener;

任何帮助,将不胜感激!请帮帮我,因为我一直在尝试解决此问题,但我的大脑没有饱和:(

让我知道您是否需要有关代码或依赖项的更多信息。

展开
收起
Puppet 2019-12-05 16:24:07 858 0
1 条回答
写回答
取消 提交回答
  • 我相信正在创建数据库,但是您似乎没有在填充它。

    使用您的Databasehelper(将addStudentDetail取消注释为唯一更改),然后使用:-

    public class MainActivity extends AppCompatActivity {
        DatabaseHelper mDBHelper;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mDBHelper = new DatabaseHelper(this);
            mDBHelper.addStudentDetail("STATE1","GRADE1","HIGH","YES","NO","NO","NO");
            mDBHelper.addStudentDetail("STATE1","GRADE2","HIGH","YES","NO","NO","NO");
            mDBHelper.addStudentDetail("STATE1","GRADE3","HIGH","YES","NO","NO","NO");
            mDBHelper.addStudentDetail("STATE1","GRADE4","HIGH","YES","NO","NO","NO");
            for (String s: mDBHelper.getData("STATE1")){
                Log.d("INFO",s);
            }
        }
    }
    

    结果是 :-

    12-05 16:38:19.211 3764-3764/? D/INFO: STATE1
    12-05 16:38:19.211 3764-3764/? D/INFO: GRADE4
    12-05 16:38:19.211 3764-3764/? D/INFO: HIGH
    12-05 16:38:19.211 3764-3764/? D/INFO: YES
    12-05 16:38:19.211 3764-3764/? D/INFO: NO
    12-05 16:38:19.211 3764-3764/? D/INFO: NO
    12-05 16:38:19.211 3764-3764/? D/INFO: NO
    

    我看不到代码中将数据添加到数据库的任何地方。相反,您只是尝试使用来获取数据String output[] = databaseHelper.getData(state);,因为没有数据,因此不会检索任何内容。

    2019-12-05 16:25:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载