我的SQLite数据库没有被创建

时间:2017-10-30 10:00:03

标签: java android database sqlite android-sqlite

下面是在数据库中创建和插入值的完整代码,但它不起作用,没有创建数据库。

MainActivity.java

   package com.dbtrial1.leenaharani.dbtrial1;
     public class MainActivity extends AppCompatActivity {

   private Button btnCreateDatabase,button;
   private MySqliteOpenHelper mySqliteOpenHelper;
   private SQLiteDatabase mDatabase ;

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

    btnCreateDatabase = (Button)findViewById(R.id.btnCreateDatabase);
    btnCreateDatabase.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            //create the database with table here
            mySqliteOpenHelper = new 
      MySqliteOpenHelper(getApplicationContext());
            mDatabase = mySqliteOpenHelper.getReadableDatabase();
            Cursor cursor = mDatabase.rawQuery("select * from Restaurant Menu;", null);
            System.out.println("Main Activity.onClick:" + cursor.getColumnCount());
        }
    } );
     button=(Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        insertData();
        }
       } );
      }

    private void insertData () {
    String BBQ="BBQ";
    String Shakes="Chocolate_Shake";
    String Deserts="Cake";
    String table_name="Restaurant_Menu";
    ContentValues values=new ContentValues();
    values.put("BBQ" , BBQ);
    values.put("Shakes",Shakes);
    values.put("Deserts",Deserts);
    long rowId =0;
    if(mDatabase!=null) {
        rowId = mDatabase.insert(table_name, null, values);
        if (rowId != -1) {
            Toast.makeText(MainActivity.this, "Inserted  Successfully!", Toast.LENGTH_SHORT).show();
        } else
            Toast.makeText(MainActivity.this, "Error Inserting!",  Toast.LENGTH_SHORT).show();
    }
        else
        Toast.makeText(MainActivity.this,"Database is 
        null",Toast.LENGTH_SHORT).show();

   } 
   }

MySqliteOpenHelper.java

package com.dbtrial1.leenaharani.dbtrial1;

public class MySqliteOpenHelper extends SQLiteOpenHelper {
private static final String database_name="Menu ";
private String table_name="Restaurant Menu";
private static final int database_version=1;
private String column_1="BBQ";
private String column_2="Shakes";
private String column_3="Deserts";
 private String create_table_statement="Create Table "+ table_name+ " ( " + column_1 + " text not null, " + column_2 +
         " TEXT, " + column_3 + " text not null ) ";
public MySqliteOpenHelper(Context context) {
    super(context, database_name, null, database_version);

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(create_table_statement);


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//changes in the database

    } }

activity_main.xml中

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.dbtrial1.leenaharani.dbtrial1.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:id="@+id/textView" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Create DAtabase"
    android:id="@+id/btnCreateDatabase"
    tools:layout_editor_absoluteX="147dp"
    tools:layout_editor_absoluteY="160dp" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="INSERT Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.153"
    app:layout_constraintRight_toLeftOf="@+id/textView"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="1.0" />

   </android.support.constraint.ConstraintLayout>

我是初学Android程序员,所以我知道我的代码没有评论/精心设计。

1 个答案:

答案 0 :(得分:3)

您的表名包含空格

这导致的命令

Create table restaurant name (bbq text not null, shakes TEXT, deserts text not null)

根据sql标准,这显然是语法错误。现在sqlite不会抛出异常,但不会因此而创建表。

所以只需将表名替换为Retaurant_Name,你就会变成黄金