当我第一次创建数据库时,我会在其中插入一些项目,但在执行此操作时我一直收到错误,我不知道为什么
01-19 11:23:56.850: E/SQLiteDatabase(11243): Error inserting manufacturer_id=0 ball=Train name=900 Global
01-19 11:23:56.850: E/SQLiteDatabase(11243): android.database.sqlite.SQLiteException: unrecognized token: ":": , while compiling: INSERT INTO content://com.tyczj.bowling.providers.balls/balls(manufacturer_id,ball,name) VALUES (?,?,?)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:146)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:367)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:253)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:111)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1737)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1610)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at com.tyczj.bowling.providers.Manufacturers$DatabaseHelper.onCreate(Manufacturers.java:65)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at com.tyczj.bowling.providers.Manufacturers.onCreate(Manufacturers.java:226)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.content.ContentProvider.attachInfo(ContentProvider.java:955)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.app.ActivityThread.installProvider(ActivityThread.java:3981)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.app.ActivityThread.installContentProviders(ActivityThread.java:3736)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at and roid.app.ActivityThread.handleBindApplication(ActivityThread.java:3692)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.app.ActivityThread.access$1200(ActivityThread.java:122)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1065)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.os.Looper.loop(Looper.java:132)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at android.app.ActivityThread.main(ActivityThread.java:4126)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at java.lang.reflect.Method.invoke(Method.java:491)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
01-19 11:23:56.850: E/SQLiteDatabase(11243): at dalvik.system.NativeStart.main(Native Method)
在我的数据库中创建我从资源获得一个字符串数组(制造商列表),并根据制造商我将另一个项目中的项目插入另一个数据库。插入基于制造商的项目时失败
这是我的制造商数据库创建一切发生的地方
@Override
public void onCreate(SQLiteDatabase db)
{
createTables(db);
String[] manufacturers = context.getResources().getStringArray(R.array.manufacturers);
ContentValues v = new ContentValues();
for(int i = 0; i < manufacturers.length; i++){
Log.d("Manufacturers",manufacturers[i]);
v.put(NAME,manufacturers[i]);
db.insert(MANUFACTURERS_TABLE,null,v);
String[] balls;
if(i == 0){
balls = context.getResources().getStringArray(R.array.global);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}else if(i == 1){
balls = context.getResources().getStringArray(R.array.amf);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}else if(i == 2){
balls = context.getResources().getStringArray(R.array.brunswick);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}else if(i == 3){
balls = context.getResources().getStringArray(R.array.columbia);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}else if(i == 4){
balls = context.getResources().getStringArray(R.array.ebonite);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}else if(i == 5){
balls = context.getResources().getStringArray(R.array.hammer);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}else if(i == 6){
balls = context.getResources().getStringArray(R.array.morich);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}else if(i == 7){
balls = context.getResources().getStringArray(R.array.rotogrip);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}else if(i == 8){
balls = context.getResources().getStringArray(R.array.storm);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}else if(i == 9){
balls = context.getResources().getStringArray(R.array.track);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}else if(i == 10){
balls = context.getResources().getStringArray(R.array.motiv);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}else if(i == 11){
balls = context.getResources().getStringArray(R.array.elite);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}else if(i == 12){
balls = context.getResources().getStringArray(R.array.dv8);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert(Balls.CONTENT_URI.toString(),null,v);
}
}
}
}
这是我插入第二个数据库
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = db.insert(BALLS_TABLE,null, values);
if(rowID > 0){
Uri _uri = ContentUris.withAppendedId(CONTENT_ID_URI_BASE,rowID);
getContext().getContentResolver().notifyChange(_uri,null);
return _uri;
}else{
throw new SQLException("Failed to insert row into " + uri);
}
}
现在我没有看到我的插页中的任何地方有:
它说的是问题,所以我不知道什么是错的。我不能这样插入吗?
修改
我的另一张桌子
private static final String DATABASE_NAME = "Balls";
private static final String BALLS_TABLE = "balls";
private static final int DATABASE_VERSION = 1;
private static final String AUTHORITY = "com.tyczj.bowling.providers.balls";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/balls");
public static final Uri CONTENT_ID_URI_BASE = Uri.parse("content://" + AUTHORITY + "/balls/");
private static final UriMatcher uriMatcher;
public static final String BALL = "ball";
public static final String ID = "_id";
public static final String MANUFACTURER_ID = "manufacturer_id";
private static final HashMap<String, String> mMap;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
我的oncreate表格
@Override
public boolean onCreate() {
Context context = getContext();
DBHelper = new DatabaseHelper(context);
db = DBHelper.getWritableDatabase();
return (db == null)? false:true;
}
编辑2:
制造商表
private void createTables(SQLiteDatabase db){
db.execSQL("CREATE TABLE " + MANUFACTURERS_TABLE + "(" + ID + " integer primary key autoincrement, " +
NAME + " TEXT);");
}
球桌
private void createTables(SQLiteDatabase db){
db.execSQL("CREATE TABLE " + BALLS_TABLE + "(" + ID + " integer primary key autoincrement, " +
MANUFACTURER_ID + " INTEGER, " + BALL + " TEXT);");
}
答案 0 :(得分:0)
我可以在这里看到“:”标记:
内容:// COM 强> .tyczj.bowling.providers.balls /球(MANUFACTURER_ID,球,名称)
答案 1 :(得分:0)
定义表时一定有问题。
检查是否正确定义了列。
如果您可以在此处发布该代码,将有助于调试。
答案 2 :(得分:0)
您需要通过直接传递表名而不是内容uri
来调用db.insert试
if(i == 0){
balls = context.getResources().getStringArray(R.array.global);
for(int j = 0; j<balls.length;j++){
Log.d("Manufacturers",balls[j]);
v.put(Balls.MANUFACTURER_ID,i);
v.put(Balls.BALL,balls[j]);
db.insert("balls",null,v);
}
}