2016-06-15 9 views
-1

この質問は何度も尋ねられていますが、私のコードになると問題が見つかりません。 :1(SQLITE_ERROR)によって引き起こさ:SQL(クエリー)エラーまたは欠落しているデータベース(「0」に近い:構文エラー(コード1):、コンパイル中:テーブルのコースを作成する(_id整数主キーの自動インクリメント、タイトルテキストnullではない、0テキストNOT NULL);)android:SQL(クエリ)エラーまたはデータベースがありません。 ( "0"の近くに:構文エラー(コード1):コンパイル中:

CourseDBAdapter.java

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

import java.util.ArrayList; 


public class CourseDBAdapter { 
private static final String DATABASE_NAME = "course.db"; 
private static final int DATABASE_VERSION = 1; 

public static final String COURSE_TABLE = "course"; 
private static final String COLUMN_ID = "_id"; 
private static final String COLUMN_TITLE = "title"; 
private static final String COLUMN_MARK = "0"; 

private String[] allColumns = {COLUMN_ID, COLUMN_TITLE, COLUMN_MARK}; 

public static final String CREATE_TABLE_COURSE = "create table " + COURSE_TABLE + " (" 
     + COLUMN_ID + " integer primary key autoincrement, " 
     + COLUMN_TITLE + " text not null, " 
     + COLUMN_MARK + " text not null " + ");"; 

private SQLiteDatabase sqlDB; 
private Context context; 
private CourseDBHelper courseDBHelper; 

public CourseDBAdapter(Context cxt) { 

    context = cxt; 
} 

public CourseDBAdapter open() throws android.database.SQLException { 

    courseDBHelper = new CourseDBHelper(context); 
    sqlDB = courseDBHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    courseDBHelper.close(); 
} 

public Course createCourse(String title) { 

    ContentValues values = new ContentValues(); 
    values.put(COLUMN_TITLE, title); 

    long insertId = sqlDB.insert(COURSE_TABLE, null, values); 

    Cursor cursor = sqlDB.query(COURSE_TABLE, allColumns, COLUMN_ID + " = " + insertId, 
      null, null, null, null); 
    cursor.moveToFirst(); 
    Course newCourse = cursorToCourse(cursor); 
    cursor.close(); 
    return newCourse; 
} 

public long deleteCourse(long idToDelete) { 
    return sqlDB.delete(COURSE_TABLE, COLUMN_ID + " = " + idToDelete, null); 

} 

public long updateCourse(long idToUpdate, String newTitle) { 

    ContentValues values = new ContentValues(); 
    values.put(COLUMN_TITLE, newTitle); 

    return sqlDB.update(COURSE_TABLE, values, COLUMN_ID + " = " + idToUpdate, null); 
} 

public ArrayList<Course> getAllCourses() { 
    ArrayList<Course> courses = new ArrayList<Course>(); 

    Cursor cursor = sqlDB.query(COURSE_TABLE, allColumns, null, null, null, null, null); 

    for (cursor.moveToLast(); !cursor.isBeforeFirst(); cursor.moveToPrevious()) { 
     Course course = cursorToCourse(cursor); 
     courses.add(course); 

    } 

    cursor.close(); 

    return courses; 
} 

public Course cursorToCourse(Cursor cursor) { 
    Course newCourse = new Course(cursor.getLong(0), cursor.getString(1), cursor.getString(2)); 
    return newCourse; 
} 

private static class CourseDBHelper extends SQLiteOpenHelper { 

    CourseDBHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

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

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

     Log.w(CourseDBHelper.class.getName(), 
       "Upgrading database from version " + oldVersion + " to " 
         + newVersion + ", which will destroy all old data"); 

     db.execSQL("DROP TABLE IF EXISTS " + COURSE_TABLE); 
     onCreate(db); 
    } 
} 

}

MainActivity.java

import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.EditText; 

public class MainActivity extends AppCompatActivity { 

public static final String COURSE_TITLE = "com.example.diba.grade1.Identifier"; 
public static final String COURSE_ID = "com.example.diba.grade1.ID"; 
public static final String COURSE_MARK = "com.example.diba.grade1.MARK"; 
public static final String COURSE_FRAGMENT_TO_LOAD = "com.example.diba.grade1.Fragment_To_Load"; 
public static Context context; 

// fixed number of well-known values in Java 
public enum FragmentToLunch{VIEW, EDIT}; 

@Override // tells the compiler that the following method overrides a method of its superclass 
protected void onCreate(Bundle savedInstanceState) { //saves current state of Activity | restores state when returned to Activity 

    super.onCreate(savedInstanceState); //super must be called when overriding | run your code in addition to the existing code in the onCreate() 
    setContentView(R.layout.activity_main); //Set the activity content from a layout resource. 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //A standard toolbar for use within application content. 
    setSupportActionBar(toolbar); //sets Toolbar to act as the ActionBar 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    //Tool bar settings and options 
    // 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; 
    }else if(id == R.id.action_add_course){ 

     //Intent provides a facility for performing late runtime binding between the code in different applications 
     final Intent intent = new Intent(this, CourseDetailActivity.class); 
     //creates alert dialog 
     //(this) - Context: Interface to global information about an application environment 
     final AlertDialog.Builder inputAlert = new AlertDialog.Builder(this); 

     inputAlert.setTitle("Add Course"); //setting alert dialog title 

     //creating EditText Widget 
     final EditText userInput = new EditText(this); 
     //Setting view of EdiText on alert dialog 
     inputAlert.setView(userInput); 
     //Setting button values for alert dialog 
     inputAlert.setPositiveButton("Save", new DialogInterface.OnClickListener(){ 

      @Override 
      public void onClick(DialogInterface dialog, int whichButton) 
      { 

       String userInputValue = userInput.getText().toString(); 

       //getBaseContext = access a Context from within another context 
       CourseDBAdapter dbAdapter = new CourseDBAdapter(getBaseContext()); 

       dbAdapter.open(); 
       dbAdapter.createCourse(userInputValue); 

       recreate(); 

       //putExtra = Add extended data to the intent. 
       intent.putExtra(MainActivity.COURSE_FRAGMENT_TO_LOAD, FragmentToLunch.VIEW); 
       startActivity(intent); 

       dbAdapter.close(); 
      } 
     }); 

     inputAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }); 

     AlertDialog alertDialog = inputAlert.create(); 
     alertDialog.show(); 
    } 

    //calls parent method to run 
    return super.onOptionsItemSelected(item); 
} 
} 

答えて

0

あなたはこの1つの

private static final String COLUMN_MARK = "0"; 

わからない数は、列名のokです置き換える必要があり、あなたが試すことができます:返信用

private static final String COLUMN_MARK = "mark"; 
+0

感謝をしかし、それを何に変更しますか? –

+0

あなたが欲しい名前が何であれ、ここでは "マーク" – Chol

+0

ありがとう!それは少し働く。しかし、今、私はエラーが出る「android.database.sqlite.SQLiteConstraintExceptionは:NOT NULL制約に失敗しました:course.zero(コード1299)」 –

関連する問題