-2

当尝试将信息添加到我的内容提供者的 SQLite 数据库时。我在我的 Logcat 上收到此错误消息。

 10-21 12:37:48.045: E/SQLiteLog(25929): (1) near "TABLECows": syntax error

我看了一遍,似乎找不到错误在哪里。当我使用调试器时,它不会给出错误的位置。你能帮我看看我缺少什么来纠正这个错误吗?

我的数据库.java

package ag.access.cowsdb;

import ag.access.cowsdb.provider.Cows_Provider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class Cowsdatabase extends SQLiteOpenHelper {

private ContentResolver cowcr;

public Cowsdatabase(Context context, String name, CursorFactory factory,
        int version) {
    super(context,DATABASE_NAME, factory, DATABASE_VERSION);
    cowcr = context.getContentResolver();
}

public static final String Key_id = "_id";
public static final String Key_Cow = "Cowid";
public static final String Key_Sire = "Sire";
public static final String Key_Dam = "Dam";
public static final String Key_DOBM = "Month";
public static final String Key_DOBD = "Date";
public static final String Key_DOBY = "Year";

private static final String DATABASE_NAME = "CowsDB";
public static final String DATABASE_TABLE = "Cows";

private static final int DATABASE_VERSION = 1;

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String Create_Cows_Table = "CREATE TABLE" + 
    DATABASE_TABLE + "(" +
    Key_id + "INTEGER PRIMARY KEY AUTOINCREMENT," +
    Key_Cow + "INTEGER NOT NULL" +
    Key_Sire + "TEXT NOT NULL," +
    Key_Dam  + "TEXT NOT NULL," +
    Key_DOBM + "INTEGER NOT NULL," +
    Key_DOBD + "INTEGER NOT NULL," +
    Key_DOBY + "INTEGER NOT NULL," + ")";
    db.execSQL(Create_Cows_Table);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
}
public void addcow(cow Cow) {
ContentValues values = new ContentValues();
values.put(Key_Cow, Cow.getCowid());
values.put(Key_Sire, Cow.getSire());
values.put(Key_Dam, Cow.getDam());
values.put(Key_DOBM, Cow.getMonth());
values.put(Key_DOBD, Cow.getDate());
values.put(Key_DOBY, Cow.getYear());

cowcr.insert(Cows_Provider.Content_Uri, values);
}
public cow findcow(int cowid) {
String[] projection = {Key_id, Key_Cow, Key_Sire, Key_Dam, Key_DOBM, Key_DOBD, Key_DOBY};

String selection = "cowid = \"" + cowid + "\"";
Cursor cursor = cowcr.query(Cows_Provider.Content_Uri, projection, selection, null, null);

cow cow = new cow();

if (cursor.moveToFirst()) {
    cursor.moveToFirst();
    cow.Setid(Integer.parseInt(cursor.getString(0)));
    cow.SetCowid(Integer.parseInt(cursor.getString(1)));
    cow.setSire(cursor.getString(2));
    cow.setDam(cursor.getString(3));
    cow.setMonth(Integer.parseInt(cursor.getString(4)));
    cow.setDate(Integer.parseInt(cursor.getString(5)));
    cow.setYear(Integer.parseInt(cursor.getString(6)));
    cursor.close();
} else {
    cow = null;
}
return cow; 
} 
public boolean deleteCow (int cowid) {
    boolean result = false;

    String selection = "cowid = \"" + cowid + "\"";

    int rowsDeleted = cowcr.delete(Cows_Provider.Content_Uri, selection, null);

    if (rowsDeleted > 0)
        result = true;

    return result;
}
}

我的内容提供者.java

package ag.access.cowsdb.provider;



import ag.access.cowsdb.Cowsdatabase;
import android.content.ContentProvider;
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;

public class Cows_Provider extends ContentProvider {

private Cowsdatabase cowsdbh;

private static final String Authority = 
        "ag.access.cowsdb.provider.Cows_Provider";
private static final String Cows = "Cows";
public static final Uri Content_Uri = Uri.parse("content://" + Authority + "/" + Cows);

public static final int COWS = 1;
public static final int COWS_ID = 2;

private static final UriMatcher cURIMatcher =
        new UriMatcher(UriMatcher.NO_MATCH);
static {
    cURIMatcher.addURI(Authority, Cows, COWS);
    cURIMatcher.addURI(Authority, Cows + "/#", COWS_ID);
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {

    int uriType = cURIMatcher.match(uri);
    SQLiteDatabase mysqldb = cowsdbh.getWritableDatabase();
    int rowsDeleted = 0;

    switch (uriType) {
    case COWS:
        rowsDeleted = mysqldb.delete(Cowsdatabase.DATABASE_TABLE, selection, selectionArgs);
        break;
    case COWS_ID:
        String id = uri.getLastPathSegment();
        if (TextUtils.isEmpty(selection)) {
            rowsDeleted = mysqldb.delete(Cowsdatabase.DATABASE_TABLE, Cowsdatabase.Key_Cow + "=" + id, null);
        } else {
        rowsDeleted = mysqldb.delete(Cowsdatabase.DATABASE_TABLE, Cowsdatabase.Key_Cow + "=" + id + " and " + selection, selectionArgs);
        }
        break;
        default:
            throw new IllegalArgumentException("Unknow URI: " + uri);
}
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsDeleted;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    int uriType = cURIMatcher.match(uri);

    SQLiteDatabase mydb = cowsdbh.getWritableDatabase();

    long id = 0;
    switch (uriType) {
    case COWS:
        id = mydb.insert(Cowsdatabase.DATABASE_TABLE, null, values);
        break;
        default:
            throw new IllegalArgumentException("Unknow Uri: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return Uri.parse(Cows + "/" + id);
}

@Override
public boolean onCreate() {
    cowsdbh = new Cowsdatabase(getContext(), null, null, 1);
    return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(Cowsdatabase.DATABASE_TABLE);

    int uriType = cURIMatcher.match(uri);

    switch (uriType) {
    case COWS_ID:
        qb.appendWhere(cowsdbh.Key_Cow + "=" + uri.getLastPathSegment());
        break;
    case COWS:
        break;
        default:
        throw new IllegalArgumentException("Unknown URI");

}

    Cursor cursor = qb.query(cowsdbh.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
        String[] selectionArgs) {
    int uriType = cURIMatcher.match(uri);
    SQLiteDatabase mysqldb = cowsdbh.getWritableDatabase();
    int rowsUpdated = 0;

    switch (uriType) {
    case COWS:
        rowsUpdated = mysqldb.update(Cowsdatabase.DATABASE_TABLE, values, selection,  selectionArgs);
        break;
    case COWS_ID:
        String id = uri.getLastPathSegment();
        if (TextUtils.isEmpty(selection)) {
            rowsUpdated =
                    mysqldb.update(Cowsdatabase.DATABASE_TABLE, values, Cowsdatabase.Key_Cow + "=" + id, null);
        } else {
            rowsUpdated =
                    mysqldb.update(Cowsdatabase.DATABASE_TABLE, values,  Cowsdatabase.Key_Cow + "=" + id + " and " + selection, selectionArgs);
        }
        break;
        default:
            throw new IllegalArgumentException("Unknown URI:" + uri);
        }
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsUpdated;
}

@Override
public String getType(Uri uri) {
    // TODO Auto-generated method stub
    return null;
}

 }

我如何以及在哪里可以纠正这个错误?

谢谢

4

1 回答 1

0

onCreate方法的第一行,添加一个空格,使其看起来像这样。

String Create_Cows_Table = "CREATE TABLE " + 
于 2013-10-21T19:59:01.923 回答