2013-05-26 253 views
0

我試圖訪問內容提供商,但我不能錯誤的,因爲:內容提供商

05-26 20:44:48.061:E/AndroidRuntime(7430):致命異常:主要 05-26 20:44:48.061:E/AndroidRuntime(7430):java.lang.RuntimeException:無法獲取提供程序com.app.mapa.MCProvider:android.database.sqlite.SQLiteException:near「TABLElugares」:語法錯誤:,在編譯:CREATE TABLElugares(_id INTEGER PRIMARY KEY AUTOINCREMENT,NOMBRE TEXT,descripcion TEXT,latitud DOUBLE,longitud DOUBLE,照片TEXT)

的代碼:

SQLiteOpenhelper類:

package com.app.mapa; 

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 Bdoh extends SQLiteOpenHelper 
{ 
SQLiteOpenHelper sbd; 
static String T_NOMBRE="lugares"; 
private static final String NOMBRE_BD = "Sitios"; 
private static final int VER_BD = 1; 
private static final String bdcons = "CREATE TABLE" + T_NOMBRE + 
"(_id INTEGER PRIMARY KEY AUTOINCREMENT, nombre TEXT, descripcion TEXT, latitud 
    DOUBLE, " + 
"longitud DOUBLE, foto TEXT)"; 

public Bdoh(Context context) 
{ 
    super(context, NOMBRE_BD, null, VER_BD); 
    SQLiteDatabase db=context.openOrCreateDatabase 
      (NOMBRE_BD,Context.MODE_PRIVATE,null); //crea la base de datos 
    // TODO Auto-generated constructor stub 
} 

@Override 
public void onCreate(SQLiteDatabase db) 
{ 
    // TODO Auto-generated method stub 
    db.execSQL(bdcons); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{ 
    // TODO Auto-generated method stub 

    db.execSQL("DROP TABLE IF EXISTS lugares"); 

    db.execSQL(bdcons); 
} 
} 

內容提供商:

package com.app.mapa; 

import android.content.ContentProvider; 
import android.content.ContentValues; 
import android.content.Context; 
import android.content.UriMatcher; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteQueryBuilder; 
import android.net.Uri; 

public class MCProvider extends ContentProvider { 

private static final String AUTH = "com.app.mapa.MCProvider"; 
private static final Uri CP_URI = Uri.parse("content://"+AUTH+Bdoh.T_NOMBRE); 
private static int COMMENT = 1; 
public static String latitud; 
public static String longitud; 

private static final UriMatcher Umatcher; 

static 
{ 
    Umatcher = new UriMatcher(UriMatcher.NO_MATCH); 
    Umatcher.addURI(AUTH, Bdoh.T_NOMBRE, COMMENT); 
} 

private SQLiteDatabase bd; 

@Override 
public boolean onCreate() 
{ 

    // TODO Auto-generated method stub 

    Context context = getContext(); 
    Bdoh bdh = new Bdoh(context); 
    bd = bdh.getWritableDatabase(); 

    return (bd == null) ? false:true; 
} 

@Override 
public Uri insert(Uri uri, ContentValues cvalues) 
{ 

    // TODO Auto-generated method stub 

    if(Umatcher.match(uri)==COMMENT) 
    { 
     bd.insert(Bdoh.T_NOMBRE, null, cvalues); 
    } 

    bd.close(); 

    getContext().getContentResolver().notifyChange(uri, null); 

    return null; 
} 

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

    // TODO Auto-generated method stub 

    Cursor cr; 

    cr = bd.query(Bdoh.T_NOMBRE, projection, selection, selectionargs, null, 
      null, sortorder); 
    cr.setNotificationUri(getContext().getContentResolver(), uri); 

    return cr; 
} 
} 

使用內容提供商:

public class MapaLugares extends FragmentActivity { 

private GoogleMap mMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_mapa_lugares); 
    final MCProvider Provc = new MCProvider(); 
    final String AUT = "com.app.mapa.MCProvider"; 
    final Uri C_URI = Uri.parse("content://"+AUT+Bdoh.T_NOMBRE); 
    final String[] p = new String[] {com.app.mapa.MCProvider.latitud, 
      com.app.mapa.MCProvider.longitud}; 

    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById 
      (R.id.map)).getMap(); 

    mMap.setOnMapClickListener(new OnMapClickListener() { 

     @Override 
     public void onMapClick(LatLng point) { 
      Projection proj = mMap.getProjection(); 
      Point coord = proj.toScreenLocation(point); 

      final String s = com.app.mapa.MCProvider.latitud + " = " 
          + point.latitude + "AND" + 
          com.app.mapa.MCProvider.longitud + " = " + point.longitude; 
      final String[] sarg=null; 
      final String sorder=null; 

      Cursor crs = getContentResolver().query(C_URI, p, 
          s, sarg, sorder); 
       } 

    }); 
} 

我怎樣才能解決這個例外?

謝謝。

+0

只注意到一個在創建table命令之後應該有一個空格。所以不用:''CREATE TABLE「+ T_NOMBRE'使用''CREATE TABLE」+ T_NOMBRE'。否則,它將如何識別什麼是表名和哪個命令。 –

回答

0

您在創建表的字符串錯誤,而不是 :

private static final String bdcons = "CREATE TABLE" + T_NOMBRE ... 

你應該做這樣的(表關鍵字後加上一個空格):

private static final String bdcons = "CREATE TABLE " + T_NOMBRE 
+0

修正了錯誤。感謝您的回答。 – axmug