2016-04-29 167 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',它就像一个魅力。