0

我正在尝试访问内容提供程序,但由于错误而无法访问:

05-26 20:44:48.061: E/AndroidRuntime(7430): 致命异常: main 05-26 20:44:48.061: E/AndroidRuntime(7430): java.lang.RuntimeException: 无法获取提供程序 com.app。 mapa.MCProvider:android.database.sqlite.SQLiteException:靠近“TABLElugares”:语法错误:,编译时:CREATE TABLElugares(_id INTEGER PRIMARY KEY AUTOINCREMENT,nombre TEXT,descripcion TEXT,latitud DOUBLE,longitud DOUBLE,foto 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);
                }

    });
}   

我该如何解决这个异常?

谢谢。

4

1 回答 1

0

您在创建表的字符串中有错误,而不是:

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

你应该这样做(在 table 关键字后添加一个空格):

private static final String bdcons = "CREATE TABLE " + T_NOMBRE
于 2013-05-26T20:14:40.873 回答