致命异性:main(SQLite数据库)

时间:2017-04-17 14:55:34

标签: java android

应用程序在启动后立即关闭。我不知道我做错了什么。请帮助!

这是主要活动: -

 package com.work.pets;
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import data.PetContract.PetEntry;
    import data.PetDbHelper;
    public class Main extends Activity {
    private PetDbHelper mDbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button addpet= (Button) findViewById(R.id.addpetbutton);
    addpet.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intent = new Intent(Main.this,Editor.class);
    startActivity(intent);
    }
    });
    mDbHelper=new PetDbHelper(this);
    }
    @Override
    protected void onStart(){
    super.onStart();
    displayDatabaseInfo();
    }
    private void displayDatabaseInfo(){
    PetDbHelper mDbHelper=new PetDbHelper(this);
    SQLiteDatabase db=mDbHelper.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM" + PetEntry.TABLE_NAME, null);
    try{
    TextView displayView= (TextView) findViewById(R.id.text_view_pet);
    displayView.setText("Number of rows" + cursor.getCount());
    }finally{
    cursor.close();
    }
    }
    private void insertPet(){
    SQLiteDatabase db= mDbHelper.getWritableDatabase();
    ContentValues values= new ContentValues();
    values.put(PetEntry.COLUMN_PET_NAME,"Toto");
    values.put(PetEntry.COLUMN_PET_BREED,"terrier");
    values.put(PetEntry.COLUMN_PET_GENDER,PetEntry.GENDER_MALE);
    values.put(PetEntry.COLUMN_PET_WEIGHT,20);
    long newRowId=db.insert(PetEntry.TABLE_NAME, null, values);
    Log.v("Main", "New row id" + newRowId);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_catalog, menu);
    return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_insert_dummy_data:
    insertPet();
    displayDatabaseInfo();
    return true;
    case R.id.action_delete_all_entries:
    return true;
    }
    return super.onOptionsItemSelected(item);
    }
    }

这是第二项活动: -

 package com.work.pets;
    import android.app.Activity;
    import android.content.ContentValues;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.support.v4.app.NavUtils;
    import android.text.TextUtils;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.Toast;
    import data.PetContract;
    import data.PetContract.PetEntry;
    import data.PetDbHelper;
    public class Editor extends Activity {
    private EditText mNameEditText;
    private EditText mBreedEditText;
    private EditText mWeightEditText;
    private Spinner mGenderSpinner;
    private int mGender=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editorview);
    mNameEditText=(EditText) findViewById(R.id.pet_name);
    mBreedEditText=(EditText) findViewById(R.id.pet_breed);
    mWeightEditText=(EditText) findViewById(R.id.pet_weight);
    mGenderSpinner=(Spinner) findViewById(R.id.spinner);
    setupSpinner();
    }
    private void setupSpinner() {
    ArrayAdapter genderspinneradapter = ArrayAdapter.createFromResource(this, R.array.array_gender_options, android.R.layout.simple_dropdown_item_1line);
    genderspinneradapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    mGenderSpinner.setAdapter(genderspinneradapter);
    mGenderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String selection = (String) parent.getItemAtPosition(position);
    if (!TextUtils.isEmpty(selection)) {
    if (selection.equals("Male")) {
    mGender = PetEntry.GENDER_MALE; // Male
    } else if (selection.equals("Female")) {
    mGender = PetEntry.GENDER_FEMALE; // Female
    } else {
    mGender = PetEntry.GENDER_UNKNOWN; // Unknown
    }
    }
    }
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    mGender=0;
    }
    });
    }
    private void insertPet(){
    String nameString = mNameEditText.getText().toString().trim();
    String breedString = mBreedEditText.getText().toString().trim();
    String weightString = mWeightEditText.getText().toString().trim();
    int weight= Integer.parseInt(weightString);
    PetDbHelper mDbHelper = new PetDbHelper(this);
    SQLiteDatabase db= mDbHelper.getWritableDatabase();
    ContentValues values= new ContentValues();
    values.put(PetEntry.COLUMN_PET_NAME,nameString);
    values.put(PetEntry.COLUMN_PET_BREED,breedString);
    values.put(PetEntry.COLUMN_PET_GENDER,mGender);
    values.put(PetEntry.COLUMN_PET_WEIGHT,weight);
    long newRowId=db.insert(PetEntry.TABLE_NAME, null, values);
    if(newRowId==-1){
    Toast.makeText(this, "Error with saving pet", Toast.LENGTH_SHORT).show();
    }else{
    Toast.makeText(this, "Pet saved with roe id:"+newRowId, Toast.LENGTH_SHORT).show();
    }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_editor, menu);
    return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_save:
    insertPet();
    finish();
    return true;
    case R.id.action_delete:
    return true;
    case android.R.id.home:
    NavUtils.navigateUpFromSameTask(this);
    return true;
    }
    return super.onOptionsItemSelected(item);
    }
    }

这是合同类: -

 package data;
    import android.provider.BaseColumns;
    public final class PetContract {
    private PetContract(){}
    public static final class PetEntry implements BaseColumns{
    public final static String TABLE_NAME="pets";
    public final static String _ID= BaseColumns._ID;
    public final static String COLUMN_PET_NAME="name";
    public final static String COLUMN_PET_BREED="breed";
    public final static String COLUMN_PET_GENDER="gender";
    public final static String COLUMN_PET_WEIGHT="weight";
    public static final int GENDER_UNKNOWN=0;
    public static final int GENDER_MALE=1;
    public static final int GENDER_FEMALE=2;
    }
    }

THIS IS THE HELPER CLASS:-

    package data;
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import data.PetContract.PetEntry;
    public class PetDbHelper extends SQLiteOpenHelper{
    private static final String DATABASE_NAME="shelter.db";
    private static final int DATABASE_VERSION=1;
    public PetDbHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
    String SQL_CREATE_PETS_TABLE="CREATE_TABLE" 
    + PetEntry.TABLE_NAME + "(" 
    + PetEntry._ID + "INTEGER PRIMARY KEY AUTOINCREMENT,"
    + PetEntry.COLUMN_PET_NAME + "TEXT NOT NULL,"
    + PetEntry.COLUMN_PET_BREED + "TEXT,"
    + PetEntry.COLUMN_PET_GENDER + "INTEGER NOT NULL,"
    + PetEntry.COLUMN_PET_WEIGHT + "INTEGER NOT NULL DEFAULT 0);";          
    db.execSQL(SQL_CREATE_PETS_TABLE);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
    }

这是错误日志: -

    04-17 14:27:56.514: E/SQLiteLog(1403): (1) near "CREATE_TABLEpets": syntax error

    04-17 14:27:56.518: D/AndroidRuntime(1403): Shutting down VM

    04-17 14:27:56.518: W/dalvikvm(1403): threadid=1: thread exiting with uncaught exception (group=0xa62bd288)

    04-17 14:27:56.518: E/AndroidRuntime(1403): FATAL EXCEPTION: main

    04-17 14:27:56.518: E/AndroidRuntime(1403): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.work.pets/com.work.pets.Main}: android.database.sqlite.SQLiteException: near "CREATE_TABLEpets": syntax error (code 1): , while compiling: CREATE_TABLEpets(_idINTEGER PRIMARY KEY AUTOINCREMENT,nameTEXT NOT NULL,breedTEXT,genderINTEGER NOT NULL,weightINTEGER NOT NULL DEFAULT 0);

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.app.ActivityThread.access$600(ActivityThread.java:130)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.os.Handler.dispatchMessage(Handler.java:99)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.os.Looper.loop(Looper.java:137)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.app.ActivityThread.main(ActivityThread.java:4745)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at java.lang.reflect.Method.invokeNative(Native Method)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at java.lang.reflect.Method.invoke(Method.java:511)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at dalvik.system.NativeStart.main(Native Method)

    04-17 14:27:56.518: E/AndroidRuntime(1403): Caused by: android.database.sqlite.SQLiteException: near "CREATE_TABLEpets": syntax error (code 1): , while compiling: CREATE_TABLEpets(_idINTEGER PRIMARY KEY AUTOINCREMENT,nameTEXT NOT NULL,breedTEXT,genderINTEGER NOT NULL,weightINTEGER NOT NULL DEFAULT 0);

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at data.PetDbHelper.onCreate(PetDbHelper.java:31)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at com.work.pets.Main.displayDatabaseInfo(Main.java:54)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at com.work.pets.Main.onStart(Main.java:47)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.app.Activity.performStart(Activity.java:5018)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)

    04-17 14:27:56.518: E/AndroidRuntime(1403):     ... 11 more

    04-17 14:27:56.522: W/Act

ivityManager(325):   Force finishing activity com.work.pets/.Main

3 个答案:

答案 0 :(得分:0)

你忘记了这一部分的空间:

String SQL_CREATE_PETS_TABLE="CREATE_TABLE" 
+ PetEntry.TABLE_NAME + "(" 

更改为:

String SQL_CREATE_PETS_TABLE="CREATE TABLE " 
+ PetEntry.TABLE_NAME + " ( " 
+ PetEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ PetEntry.COLUMN_PET_NAME + " TEXT NOT NULL, "
+ PetEntry.COLUMN_PET_BREED + " TEXT, "
+ PetEntry.COLUMN_PET_GENDER + " INTEGER NOT NULL, "
+ PetEntry.COLUMN_PET_WEIGHT + " INTEGER NOT NULL DEFAULT 0)";          
db.execSQL(SQL_CREATE_PETS_TABLE);

答案 1 :(得分:0)

A。使用CREATE TABLE代替CREATE_TABLE

B。在语句

之间添加空格

更新您的PetDbHelper课程,如下所示:

package data;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import data.PetContract.PetEntry;
public class PetDbHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME="shelter.db";
private static final int DATABASE_VERSION=1;
public PetDbHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    String SQL_CREATE_PETS_TABLE = "CREATE TABLE " 
    + PetEntry.TABLE_NAME + " ( " 
    + PetEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
    + PetEntry.COLUMN_PET_NAME + " TEXT NOT NULL, "
    + PetEntry.COLUMN_PET_BREED + " TEXT, "
    + PetEntry.COLUMN_PET_GENDER + " INTEGER NOT NULL, "
    + PetEntry.COLUMN_PET_WEIGHT + " INTEGER NOT NULL DEFAULT 0)";          

    db.execSQL(SQL_CREATE_PETS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}

答案 2 :(得分:0)

您的代码中存在语法错误

  

04-17 14:27:56.514:E / SQLiteLog(1403):( 1)接近&#34; CREATE_TABLEpets&#34;:   语法错误

SQL CREATE TABLE 语句语法:

CREATE TABLE table_name (
    column1 datatype,
    column2 datatype,
    column3 datatype,
   ....
);

你写的:

CREATE_TABLEpets(_idINTEGER PRIMARY KEY AUTOINCREMENT,nameTEXT NOT NULL,breedTEXT,genderINTEGER NOT NULL,weightINTEGER NOT NULL DEFAULT 0);

正确添加空格并将CREATE_TABLE更改为CREATE TABLE

以下是Android文档的示例

private static final String SQL_CREATE_ENTRIES =
    "CREATE TABLE " + FeedEntry.TABLE_NAME + " (" + 
    FeedEntry._ID + " INTEGER PRIMARY KEY," + 
    FeedEntry.COLUMN_NAME_TITLE + " TEXT," + 
    FeedEntry.COLUMN_NAME_SUBTITLE + " TEXT)";