2012-12-16 3 views
0

데이터베이스를 만들려고하지만 작동하지 않습니다 (앱을 강제 중지해야 함). 내가 로그 고양이를 확인하려고하면 오류가 "투영에서 알 수없는 열"이며 MyToDoContentProvider.java를 확인하는 것이 좋습니다. 내 SQLite 문으로 MyToDoContentProvider.java를 이미 검사 할 것이지만 무엇이 잘못되었는지는 모른다. 누구든지 나를 도울 수 있습니까? TQ .. 투영 중 알 수없는 열이 SQLite에 나타납니다

내 MyToDoContentProvider.java

package com.date.contentprovider; 

import java.util.Arrays; 
import java.util.HashSet; 

import android.content.ContentProvider; 
import android.content.ContentResolver; 
import android.content.ContentValues; 
import android.content.UriMatcher; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteQueryBuilder; 
import android.net.Uri; 
import android.text.TextUtils; 
import com.date.database.FirstClass; 
import com.date.database.TodoDatabaseHelper; 

public class MyTodoContentProvider extends ContentProvider { 

// database 
private TodoDatabaseHelper database; 

// Used for the UriMacher 
private static final int TODOS = 10; 
private static final int TODO_ID = 20; 

private static final String AUTHORITY = "com.date.contentprovider"; 

private static final String BASE_PATH = "todos"; 
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY 
    + "/" + BASE_PATH); 

public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE 
    + "/todos"; 
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE 
    + "/todo"; 

private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH); 
static { 
sURIMatcher.addURI(AUTHORITY, BASE_PATH, TODOS); 
sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", TODO_ID); 
} 

@Override 
public boolean onCreate() { 
database = new TodoDatabaseHelper(getContext()); 
return false; 
} 

@Override 
public Cursor query(Uri uri, String[] projection, String selection, 
    String[] selectionArgs, String sortOrder) { 

// Uisng SQLiteQueryBuilder instead of query() method 
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); 

// Check if the caller has requested a column which does not exists 
checkColumns(projection); 

// Set the table 
queryBuilder.setTables(FirstClass.TABLE_TODO); 

int uriType = sURIMatcher.match(uri); 
switch (uriType) { 
case TODOS: 
    break; 
case TODO_ID: 
    // Adding the ID to the original query 
    queryBuilder.appendWhere(FirstClass.COLUMN_ID + "=" 
     + uri.getLastPathSegment()); 
    break; 
default: 
    throw new IllegalArgumentException("Unknown URI: " + uri); 
} 

SQLiteDatabase db = database.getWritableDatabase(); 
Cursor cursor = queryBuilder.query(db, projection, selection, 
    selectionArgs, null, null, sortOrder); 
// Make sure that potential listeners are getting notified 
cursor.setNotificationUri(getContext().getContentResolver(), uri); 

return cursor; 
} 

@Override 
public String getType(Uri uri) { 
return null; 
} 

@Override 
public Uri insert(Uri uri, ContentValues values) { 
int uriType = sURIMatcher.match(uri); 
SQLiteDatabase sqlDB = database.getWritableDatabase(); 
int rowsDeleted = 0; 
long id = 0; 
switch (uriType) { 
case TODOS: 
    id = sqlDB.insert(FirstClass.TABLE_TODO, null, values); 
    break; 
default: 
    throw new IllegalArgumentException("Unknown URI: " + uri); 
} 
getContext().getContentResolver().notifyChange(uri, null); 
return Uri.parse(BASE_PATH + "/" + id); 
} 

@Override 
public int delete(Uri uri, String selection, String[] selectionArgs) { 
int uriType = sURIMatcher.match(uri); 
SQLiteDatabase sqlDB = database.getWritableDatabase(); 
int rowsDeleted = 0; 
switch (uriType) { 
case TODOS: 
    rowsDeleted = sqlDB.delete(FirstClass.TABLE_TODO, selection, 
     selectionArgs); 
    break; 
case TODO_ID: 
    String id = uri.getLastPathSegment(); 
    if (TextUtils.isEmpty(selection)) { 
    rowsDeleted = sqlDB.delete(FirstClass.TABLE_TODO, 
      FirstClass.COLUMN_ID + "=" + id, 
     null); 
    } else { 
    rowsDeleted = sqlDB.delete(FirstClass.TABLE_TODO, 
      FirstClass.COLUMN_ID + "=" + id 
     + " and " + selection, 
     selectionArgs); 
    } 
    break; 
default: 
    throw new IllegalArgumentException("Unknown URI: " + uri); 
} 
getContext().getContentResolver().notifyChange(uri, null); 
return rowsDeleted; 
} 

@Override 
public int update(Uri uri, ContentValues values, String selection, 
    String[] selectionArgs) { 

int uriType = sURIMatcher.match(uri); 
SQLiteDatabase sqlDB = database.getWritableDatabase(); 
int rowsUpdated = 0; 
switch (uriType) { 
case TODOS: 
    rowsUpdated = sqlDB.update(FirstClass.TABLE_TODO, 
     values, 
     selection, 
     selectionArgs); 
    break; 
case TODO_ID: 
    String id = uri.getLastPathSegment(); 
    if (TextUtils.isEmpty(selection)) { 
    rowsUpdated = sqlDB.update(FirstClass.TABLE_TODO, 
     values, 
     FirstClass.COLUMN_ID + "=" + id, 
     null); 
    } else { 
    rowsUpdated = sqlDB.update(FirstClass.TABLE_TODO, 
     values, 
     FirstClass.COLUMN_ID + "=" + id 
     + " and " 
     + selection, 
     selectionArgs); 
    } 
    break; 
default: 
    throw new IllegalArgumentException("Unknown URI: " + uri); 
} 
getContext().getContentResolver().notifyChange(uri, null); 
return rowsUpdated; 
} 

private void checkColumns(String[] projection) { 
String[] available = { FirstClass.COLUMN_NAME, 
     FirstClass.COLUMN_DESCRIPTION, FirstClass.COLUMN_CATEGORY,  FirstClass.COLUMN_TRIGGER, 
     FirstClass.COLUMN_MON, FirstClass.COLUMN_TUE, FirstClass.COLUMN_WED, FirstClass.COLUMN_THU, 
     FirstClass.COLUMN_FRI, FirstClass.COLUMN_SAT, FirstClass.COLUMN_SUN, FirstClass.COLUMN_DATE, 
     FirstClass.COLUMN_TIME, FirstClass.COLUMN_LATITUDE, FirstClass.COLUMN_LONGITUDE, 
     FirstClass.COLUMN_STATUS }; 


if (projection != null) { 
    HashSet<String> requestedColumns = new HashSet<String>(Arrays.asList(projection)); 
    HashSet<String> availableColumns = new HashSet<String>(Arrays.asList(available)); 
    // Check if all columns which are requested are available 
    if (!availableColumns.containsAll(requestedColumns)) { 
    throw new IllegalArgumentException("Unknown columns in projection"); 
    } 
} 

}

}

이며,이 내 FirstClass.java

package com.date.database; 

import android.database.sqlite.SQLiteDatabase; 
import android.util.Log; 

public class FirstClass { 
// Database table 
    public static final String TABLE_TODO = "todo"; 
    public static final String COLUMN_ID = "_id"; 
    public static final String COLUMN_CATEGORY = "category"; 
    public static final String COLUMN_NAME = "name"; 
    public static final String COLUMN_DESCRIPTION = "description"; 
    public static final String COLUMN_TRIGGER ="trigger"; 
    public static final String COLUMN_MON = "mon"; 
    public static final String COLUMN_TUE = "tue"; 
    public static final String COLUMN_WED = "wed"; 
    public static final String COLUMN_THU = "thu"; 
    public static final String COLUMN_FRI ="fri"; 
    public static final String COLUMN_SAT = "sat"; 
    public static final String COLUMN_SUN = "sun"; 
    public static final String COLUMN_DATE = "date"; 
    public static final String COLUMN_TIME = "time"; 
    public static final String COLUMN_LATITUDE ="latitude"; 
    public static final String COLUMN_LONGITUDE= "longitude"; 
    public static final String COLUMN_STATUS = "status"; 



    // Database creation SQL statement 
    private static final String DATABASE_CREATE = "create table " 
     + TABLE_TODO 
     + "(" 
     + COLUMN_ID + " integer primary key autoincrement, " 
     + COLUMN_CATEGORY + " text not null, " 
     + COLUMN_NAME + " text not null," 
     + COLUMN_DESCRIPTION + " text not null," 
     + COLUMN_TRIGGER + " text not null ,"//'t', " 
     + COLUMN_MON + " integer not null , "// == 1," 
     + COLUMN_TUE + " integer not null , "//== 2," 
     + COLUMN_WED + " integer not null , "// == 3," 
     + COLUMN_THU + " integer not null , "// == 4," 
     + COLUMN_FRI + " integer not null , "// == 5," 
     + COLUMN_SAT + " integer not null , "// == 6," 
     + COLUMN_SUN + " integer not null , "// == 0," 
     + COLUMN_DATE + " date not null," 
     + COLUMN_TIME + " time not null," 
     + COLUMN_LATITUDE + " decimal not null , "// ==0.00000000," 
     + COLUMN_LONGITUDE + " decimal not null , "// ==0.00000000," 
     + COLUMN_STATUS + " text not null "// 'act' " 
     + ");"; 

    public static void onCreate(SQLiteDatabase database) { 
    database.execSQL(DATABASE_CREATE); 

    } 

    public static void onUpgrade(SQLiteDatabase database, int oldVersion, 
     int newVersion) { 
    Log.w(FirstClass.class.getName(), "Upgrading database from version " 
     + oldVersion + " to " + newVersion 
     + ", which will destroy all old data"); 
    database.execSQL("DROP TABLE IF EXISTS " + TABLE_TODO); 
    // database.execSQL("INSERT INTO "+TABLE_TODO+" VALUES (null, datetime()) "); 
    onCreate(database); 
    } 

}

이있다

package com.date.database; 


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

public class TodoDatabaseHelper extends SQLiteOpenHelper { 

    private static final String DATABASE_NAME = "todotable.db"; 
    private static final int DATABASE_VERSION = 1; 
    final int oldVersion =1; 
    final int newVersion =1; 


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

    // Method is called during creation of the database 
    @Override 
    public void onCreate(SQLiteDatabase database) { 
    FirstClass.onCreate(database); 
    } 

    // Method is called during an upgrade of the database, 
    // e.g. if you increase the database version 
    @Override 
    public void onUpgrade(SQLiteDatabase database, int oldVersion, 
     int newVersion) { 
    FirstClass.onUpgrade(database, oldVersion, newVersion); 
    } 
} 

답변

1

확인 내 TodoDatabaseHelper.java 데이터베이스를 쿼리하는 지점입니다. 해당 테이블에 존재하지 않는 열을 요청해야합니다.

+0

내가 묻는 모든 열은 이미 테이블에 선언되어 있습니다. 왜 내가 매우 혼란 스럽습니다. – user1858863

+0

ToDoDatabaseHelper에 대한 코드를 추가 할 수 있습니까? –

+0

이미 추가했습니다. 위에서 추가를 볼 수 있습니다 .. – user1858863

관련 문제