2016-04-29 154 views
0

我有一個應用程序,我試圖在使用SQLlite創建的表內添加用戶信息。問題是當我按下應用程序崩潰的添加按鈕時。波紋管我包括所有的代碼和logcat。SQLlite導致按鈕按下時崩潰

MainActivity.java

package com.example.asus.sqlliteproject; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 
    DataBaseHelper myDB; 
    EditText Name,LastName,Grades; 
    Button AddData; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     myDB = new DataBaseHelper(this); 
     Name = (EditText)findViewById(R.id.name); 
     LastName = (EditText)findViewById(R.id.lastName); 
     Grades = (EditText)findViewById(R.id.Grades); 
     AddData = (Button)findViewById(R.id.addDataButton); 
     addData(); 
    } 
    public void addData() { 
     AddData.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       boolean inserted = myDB.insertData(Name.getText().toString(), 
         LastName.getText().toString(), 
         Grades.getText().toString()); 
       if (inserted){ 
        Toast.makeText(MainActivity.this,"Text Inserted!", Toast.LENGTH_SHORT).show(); 
       } else 
        Toast.makeText(MainActivity.this,"Unsuccessful!", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

    @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); 
    } 
} 

DataBaseHelper.java

package com.example.asus.sqlliteproject; 

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

/** 
* Created by Asus on 29.4.2016. 
*/ 
public class DataBaseHelper extends SQLiteOpenHelper { 

    public static final String DataBaseName = "student.db"; 
    public static final String DataTableName = "studentTable.db"; 
    public static final String ColID = "ID"; 
    public static final String ColName = "Name"; 
    public static final String ColLastName = "LastName"; 
    public static final String ColGrades = "Grade"; 

    public DataBaseHelper(Context context) { 
     super(context, DataBaseName, null, 1); 
     //SQLiteDatabase db = this.getWritableDatabase(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("create table" + DataTableName + "(ID INTEGER PRIMARY KEY AUTOINCREMENT)," + 
       "Name TEXT, LastName TEXT, Grade INTEGER"); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("Drop table if exists" + DataTableName); 
     onCreate(db); 
    } 
    public boolean insertData (String name, String LastName, String Grades) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(ColName,name); 
     contentValues.put(ColLastName,LastName); 
     contentValues.put(ColGrades,Grades); 
     long result = db.insert(DataTableName, null, contentValues); 
     if (result == -1) { 
      return false; 
     }else 
      return true; 
    } 
} 

logcat的錯誤(分離成兩個IMGS)和activity_main視圖

logcat1

logcat2

enter image description here

+0

檢查你是否不忘記某處的空間......所以不是「在我的代碼中查找錯字」服務 – Selvin

+1

從表名中刪除.db擴展名。即只使用studentTable作爲你的表名 – Meenaxi

+0

@selvin檢查了空格,他們似乎沒問題 – zamzam

回答

2

你錯過了空白這裏「創建表」應該「創建表」

不要忘了空格。

試試這個:

public class DataBaseHelper extends SQLiteOpenHelper { 

    public static final String DataBaseName = "student.db"; 
    public static final String DataTableName = "studentTable"; 
    public static final String ColID = "ID"; 
    public static final String ColName = "Name"; 
    public static final String ColLastName = "LastName"; 
    public static final String ColGrades = "Grade"; 

    public DataBaseHelper(Context context) { 
     super(context, DataBaseName, null, 1); 
     //SQLiteDatabase db = this.getWritableDatabase(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("create table " + DataTableName + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       "Name TEXT, LastName TEXT, Grade INTEGER);"); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("Drop table if exists" + DataTableName); 
     onCreate(db); 
    } 
    public boolean insertData (String name, String LastName, String Grades) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put(ColName,name); 
     contentValues.put(ColLastName,LastName); 
     contentValues.put(ColGrades,Grades); 
     long result = db.insert(DataTableName, null, contentValues); 
     if (result == -1) { 
      return false; 
     }else 
      return true; 
    } 

} 
+2

另一個在'(ID INTEGER PRIMARY KEY AUTOINCREMENT)'之前缺少'。 – Berger

+0

我添加了空格db.execSQL(「create table」+ DataTableName +「(ID INTEGER PRIMARY KEY AUTOINCREMENT),」+仍然沒有變化 – zamzam

+0

請求更新堆棧跟蹤,您是否在逗號後添加空格?**(ID INTEGER 「** – Alexander

0

一旦我得到類似的錯誤。我簡單地用'_id'取代了'ID',它就像一個魅力。