我尝试打开sqlite数据库后,我的android应用程序崩溃

时间:2015-08-05 13:25:25

标签: android database sqlite crash

当我填写完所有表单并尝试创建数据库时,我的应用程序崩溃了。

要创建数据库,我与youtube教程密切合作:https://www.youtube.com/watch?v=zH7dmLjUrPA

我缝制了每次来到这一行的debuger(SQLiteDatabase SQ = dop.getWritableDatabase(); - 在DataOperations.java)应用程序崩溃。

继承我的代码: 的 DatabaseOperations.java:

cbuffer ConstantBuffer : register(b0)
{
    matrix view;
    matrix projection;
    float4 color;
}

struct VOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};

VOut main(float4 position : POSITION)
{
    VOut output;

    output.position = mul(mul(position, view), projection);
    output.color = color;

    return output;
}

NewCourseActivity.java:

package com.example.ido.grades;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseOperations extends SQLiteOpenHelper {
public static final int database_version =1;
public String CREATE_QUERY = "CREATE_TABLE " + TableData.TableInfo.TABLE_NAME + "("+ TableData.TableInfo.YEAR+" TEXT,"
        + TableData.TableInfo.SEMESTER+" TEXT,"+ TableData.TableInfo.COURSE+" TEXT,"+ TableData.TableInfo.POINTS+" TEXT,"
        + TableData.TableInfo.GRADE+" TEXT);";
public DatabaseOperations(Context context) {
    super(context, TableData.TableInfo.DATABASE_NAME , null, database_version);
    Log.d("DatabaseOperations", "DataBase created");
}

@Override
public void onCreate(SQLiteDatabase sdb) {

    sdb.execSQL(CREATE_QUERY);
    Log.d("DatabaseOperations", "Table created");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
public void putInformation(DatabaseOperations dop, String year, String semester, String course, String grade, String points){

    SQLiteDatabase SQ =  dop.getWritableDatabase();  //  <------The line that causes crash
    ContentValues cv = new ContentValues();
    cv.put(TableData.TableInfo.YEAR, year);
    cv.put(TableData.TableInfo.SEMESTER, semester);
    cv.put(TableData.TableInfo.COURSE, course);
    cv.put(TableData.TableInfo.POINTS,points);
    cv.put(TableData.TableInfo.GRADE,grade);

    long k =  SQ.insert(TableData.TableInfo.TABLE_NAME, null, cv);
    Log.d("DatabaseOperations", "One raw inserted");

}
}

TebleData.java:

package com.example.ido.grades;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class NewCourseActivity extends ActionBarActivity {
EditText COURSE, POINTS, GRADE;
Spinner YEAR, SEMESTER;
String year, semester, course,  points, grade;
Context ctx;


ArrayAdapter<String> yearAdapter;
ArrayAdapter<String> SemesterAdapter;
protected void onCreate(Bundle savedInstanceState) {
    ctx=this;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_course);
    COURSE= (EditText)findViewById(R.id.courseText);
    GRADE= (EditText)findViewById(R.id.gradeText);
    POINTS= (EditText)findViewById(R.id.pointsText);

    YEAR = (Spinner)findViewById(R.id.spinnerYear);
    String[] yearItems = new String[]{"select year","1", "2", "3"};
     yearAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, yearItems);
    YEAR.setAdapter(yearAdapter);

    SEMESTER = (Spinner)findViewById(R.id.spinnerSemester);
    String[] SemesterItems = new String[]{"select semeste","a'", "b'", "c"};
    SemesterAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, SemesterItems);
    SEMESTER.setAdapter(SemesterAdapter);



    }

    public void save (View view){
         year = YEAR.getSelectedItem().toString();
         semester =SEMESTER.getSelectedItem().toString();
         course = COURSE.getText().toString();
         points= POINTS.getText().toString();
         grade = GRADE.getText().toString();

        if (year==null || year =="select year"){
            Toast.makeText(this, "select year",Toast.LENGTH_SHORT).show();
        }
        else  if (semester==null || semester=="select semester"){
            Toast.makeText(this, "select semester",Toast.LENGTH_SHORT).show();
        }
        else  if (course.equals("")){
            Toast.makeText(this, "select course",Toast.LENGTH_SHORT).show();
        }
        else  if (Integer.parseInt(points)>6 && Integer.parseInt(points) <0){
            Toast.makeText(this, "points between 0-6.",Toast.LENGTH_SHORT).show();
        }
        else  if (Integer.parseInt(grade)>100 && Integer.parseInt(grade) <0){
            Toast.makeText(this, "grade between 0-100.",Toast.LENGTH_SHORT).show();
        }

        else {

           DatabaseOperations DB  = new DatabaseOperations(NewCourseActivity.this);
            DB.putInformation(DB,year,semester,course,points,grade);
          //  DB.putInformation(DB, year, semester, course, points, grade);


            Toast.makeText(this, "saved succesfully", Toast.LENGTH_SHORT).show();
            Intent intent  = new  Intent (NewCourseActivity.this,MainActivity.class);
            finish();
            startActivity(intent);

        }
    }
    }

logcat的:

package com.example.ido.grades;
import android.provider.BaseColumns;
public class TableData {
public TableData(){

}
public static abstract class TableInfo implements BaseColumns{
public static final String YEAR = "year";
public static final String SEMESTER = "semester";
public static final String COURSE = "course";
public static final String  POINTS= "points";
public static final String GRADE = "grade";
public static final String DATABASE_NAME = "add_new_course";
public static final String TABLE_NAME = "add_course";
}
}

谢谢!!!

2 个答案:

答案 0 :(得分:1)

这是你的错误

DatabaseOperations DB  = new DatabaseOperations(Rigstretion.this);

将此行更改为

DatabaseOperations DB  = new DatabaseOperations(NewCourseActivity.this);

答案 1 :(得分:1)

near "CREATE_TABLE": syntax error (code 1): , while compiling: CREATE_TABLE add_course(year TEXT,semester TEXT,course TEXT,points TEXT,grade TEXT);

使用_中的常规空格替换下划线CREATE TABLE以修复语法错误。