如何在android java中修复sqliteException

Posted

技术标签:

【中文标题】如何在android java中修复sqliteException【英文标题】:how to fix sqliteException in android java 【发布时间】:2019-12-06 12:12:13 【问题描述】:

我试图将值插入数据库以及内容提供程序中,但得到 null sqlite 异常。在使用内容提供程序类之前,我的数据被添加到数据库中没有任何问题。尝试内容提供程序后我得到异常。另外我不知道是否为内容提供者插入了值..... 在 SpecificActivity 类中,我需要使用由 LoadImageAsyncTask 类返回的drawable,但我在那里得到空值...... 帮我解决这些问题..

public class FeedReaderContract 
    public static class FeedEntry implements BaseColumns 
        public static final String TABLE_NAME = "entry";
        public static final String _ID = BaseColumns._ID;
        public static final String COLUMN_NAME_TITLE = "title";
        public static final String COLUMN_NAME_CONTENT = "content";
        public static final String COLUMN_NAME_IMAGE = "image";
        public static final String COLUMN_NAME_CHANNEL = "channel";

    

public class FeedReaderDbHelper extends SQLiteOpenHelper 

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "FeedReader.db";
    Context context;
    public Context getContext() 
        return context;
    
    private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE " + FeedReaderContract.FeedEntry.TABLE_NAME + " (" +
        FeedReaderContract.FeedEntry._ID + "  INTEGER PRIMARY KEY, " +
        FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE + "  TEXT, " +
        FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT + "  TEXT, " +
        FeedReaderContract.FeedEntry.COLUMN_NAME_CHANNEL + "  TEXT, " +
        FeedReaderContract.FeedEntry.COLUMN_NAME_IMAGE + "  TEXT); ";

    private static final String SQL_DELETE_ENTRIES =
        "DROP TABLE IF EXISTS " + FeedReaderContract.FeedEntry.TABLE_NAME;
    public FeedReaderDbHelper(Context context) 
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    
    public void onCreate(SQLiteDatabase db) 
        NewsFeedProvider.db = db;
        db.execSQL(SQL_CREATE_ENTRIES);
        Log.e("sql db", "created");

    
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) 
        db.execSQL("DROP TABLE IF EXISTS " + FeedReaderContract.FeedEntry.TABLE_NAME);
        onCreate(db);
    
    public void getData(long id) 
        try 
            Log.e("entered", "getdata");

            SQLiteDatabase db = getReadableDatabase();
            Cursor cursor = db.query(FeedReaderContract.FeedEntry.TABLE_NAME, new String[] 
                FeedReaderContract.FeedEntry._ID, FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, FeedReaderContract.FeedEntry.COLUMN_NAME_CHANNEL, FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT, FeedReaderContract.FeedEntry.COLUMN_NAME_IMAGE
            , FeedReaderContract.FeedEntry._ID + "=?", new String[] 
                String.valueOf(id)
            , null, null, null, null);
            ArrayList < NewsReport > newsReports = new ArrayList < > ();
            List itemIds = new ArrayList < > ();
            while (cursor.moveToNext()) 

                newsReports.add(new NewsReport(cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE)),
                    cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_CHANNEL)),
                    cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT)),
                    cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_NAME_IMAGE))));
                long itemId = cursor.getLong(
                    cursor.getColumnIndexOrThrow(FeedReaderContract.FeedEntry._ID));
                itemIds.add(itemId);
                Log.e("id", "" + itemId);
            
            cursor.close();
            Log.e("newsreportsize", "dbhelper" + newsReports.size());
            if (newsReports.size() != 0) 
                for (int i = 0; i < newsReports.size(); i++) 
                    Log.e("news pic", "" + newsReports.get(i).getPic());
                    Log.e("news title", "" + newsReports.get(i).getTitle());
                    Log.e("news content", "" + newsReports.get(i).getContent());
                    Log.e("news chanel", "" + newsReports.get(i).getNewsChannel());
                
            

         catch (Exception ex) 
            Log.e("exception req data", "" + ex);
        
    

public class LoadImageAsyncTask extends AsyncTask < String, Void, Drawable > 
    private Drawable drawable;
    String imageUrl;
    private Drawable image;
    @Override
    protected Drawable doInBackground(String...strings) 
        try 
            InputStream is = (InputStream) new URL(imageUrl).getContent();
            Drawable d = Drawable.createFromStream(is, "src name");

            Log.e("drwable", "" + d);


         catch (Exception e) 
            Log.e("Specific Activity", "Converting drawable" + e);

        
        return drawable;
    

    @Override
    protected void onPostExecute(Drawable drawableImage) 
        super.onPostExecute(drawableImage);
        setImage(drawableImage);

    
    public void setImage(Drawable drawable) 
        new SpecificNewsReportActivity().drawable = drawable;
        Log.e("set image", "" + drawable);
        this.drawable = drawable;
    
    public Drawable getImage() 
        Log.e("get image", "" + drawable);

        return drawable;
    

public class NewsFeedProvider extends ContentProvider 
    static final String PROVIDER_NAME = "com.example.newsreport";
    static final String URL = "content://" + PROVIDER_NAME + "/newsfeed";
    static final Uri CONTENT_URL = Uri.parse(URL);
    static final int uriCode = 1;
    static String newsTitle;
    static String newsContent;
    static String newsImage;
    static String newsChannel;
    static final UriMatcher uriMatcher;
    private FeedReaderDbHelper dbHelper;
    private static HashMap < String, String > values;
    public static SQLiteDatabase db;
    static 
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(PROVIDER_NAME, "newsfeed", uriCode);
    
    @Override
    public boolean onCreate() 

        dbHelper = new FeedReaderDbHelper(getContext());
        return true;
    

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) 
        SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
        queryBuilder.setTables(FeedReaderContract.FeedEntry.TABLE_NAME);
        switch (uriMatcher.match(uri)) 
            case uriCode:
                queryBuilder.setProjectionMap(values);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI" + uri);
        
        //Cursor cursor=queryBuilder.query(dbHelper.getReadableDatabase(),projection,selection,selectionArgs,null,null,sortOrder);
        Cursor cursor = dbHelper.getReadableDatabase().query(FeedReaderContract.FeedEntry.TABLE_NAME, projection, FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE + "= ?", selectionArgs, null, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    

    @Override
    public String getType(Uri uri) 
        switch (uriMatcher.match(uri)) 
            case uriCode:
                return "vnd.android.cursor.dir/newsfeed";

            default:
                throw new IllegalArgumentException("Unsupported URI" + uri);
        

    

    @Override
    public Uri insert(Uri uri, ContentValues contentValues) 
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        long rowId = db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, contentValues);
        if (rowId > 0) 
            Uri _uri = ContentUris.withAppendedId(CONTENT_URL, rowId);
            getContext().getContentResolver().notifyChange(_uri, null);
            Log.e("insert", "feedreader" + contentValues);
            return _uri;
         else 
            Toast.makeText(getContext(), "Row insert failed", Toast.LENGTH_SHORT).show();
            return null;
        
    

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) 
        int rowsDeleted = 0;
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) 
            case uriCode:
                rowsDeleted = db.delete(FeedReaderContract.FeedEntry.TABLE_NAME, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI" + uri);
        
        getContext().getContentResolver().notifyChange(uri, null);
        return rowsDeleted;
    

    @Override
    public int update(Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) 
        int rowsUpdated = 0;
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) 
            case uriCode:
                rowsUpdated = db.delete(FeedReaderContract.FeedEntry.TABLE_NAME, selection, selectionArgs);
                break;
            default:
                throw new IllegalArgumentException("Unknown URI" + uri);
        
        getContext().getContentResolver().notifyChange(uri, null);
        return rowsUpdated;
    


public class NewsReportActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks < ArrayList < NewsReport >> 
    ProgressBar progressBar;
    FeedReaderDbHelper dbHelper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycler_view);
        dbHelper = new FeedReaderDbHelper(this);
        db = dbHelper.getWritableDatabase();
        Log.e("application", "" + dbHelper);
        Intent intent = getIntent();
        if (intent.hasExtra("exception")) 
            TextView connectionTextView = (TextView) findViewById(R.id.no_connection_text_view);
            connectionTextView.setText("There is no internet connection!!!");
         else 
            Log.e("no exception", "entered else");
        
        getSupportLoaderManager().initLoader(0, null, this);

    

    @NonNull
    @Override
    public Loader < ArrayList < NewsReport >> onCreateLoader(int id, @Nullable Bundle args) 
        NewsReportLoader newsReportLoader = new NewsReportLoader(this);
        newsReportLoader.forceLoad();
        progressBar = (ProgressBar) findViewById(R.id.progress_bar);
        progressBar.setVisibility(View.VISIBLE);

        return newsReportLoader;
    

    @Override
    public void onLoadFinished(@NonNull Loader < ArrayList < NewsReport >> loader, ArrayList < NewsReport > data) 
        try 
            Log.e("data", "" + data.size());
            NewsReportAdapter adapter = new NewsReportAdapter(this, data);
            RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
            progressBar.setVisibility(View.INVISIBLE);
            recyclerView.setAdapter(adapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            DividerItemDecoration mDividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
                new LinearLayoutManager(this).getOrientation());
            recyclerView.addItemDecoration(mDividerItemDecoration);

            if (dbHelper != null) 
                Log.e("dbhelper", "notnull");
                db = dbHelper.getWritableDatabase();
                if (db != null) 
                    Log.e("db", "notnull");
                    for (int i = 0; i < data.size(); i++) 
                        ContentValues values = new ContentValues();
                        NewsReport newsReport = data.get(i);
                        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, newsReport.getTitle());
                        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT, newsReport.getContent());
                        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CHANNEL, newsReport.getNewsChannel());
                        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_IMAGE, newsReport.getPic());
                        values.put(NewsFeedProvider.newsTitle, newsReport.getTitle());
                        values.put(NewsFeedProvider.newsContent, newsReport.getContent());
                        values.put(NewsFeedProvider.newsChannel, newsReport.getNewsChannel());
                        values.put(NewsFeedProvider.newsImage, newsReport.getPic());
                        try 
                            Uri uri = getApplicationContext().getContentResolver().insert(NewsFeedProvider.CONTENT_URL, values);
                         catch (Exception ex) 
                            Log.e("exception in resolver" + getContentResolver(), "" + ex);
                        
                        long newRowId = db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);
                        Log.e("new row id", "" + newRowId);
                        dbHelper.getData(newRowId);
                    
                 else
                    Log.e("db", "null");
             else 
                Log.e("dbhelper", "null");
            
            // db.execSQL("delete from "+ FeedReaderContract.FeedEntry.TABLE_NAME);

         catch (Exception ex) 
            Log.e("newsactivity", "" + FeedReaderContract.FeedEntry.TABLE_NAME.length());
            Log.e("onloadfinished", "" + ex);
            Log.e("datahelper", "" + dbHelper.getReadableDatabase().rawQuery("SELECT * FROM " + FeedReaderContract.FeedEntry.TABLE_NAME, null));
        
    

    @Override
    public void onLoaderReset(@NonNull Loader < ArrayList < NewsReport >> loader) 

    

public class SpecificNewsReportActivity extends AppCompatActivity 
    private TextView contentTextView, titleTextView;
    public ImageView imageView;

    private Intent intent;
    public Drawable drawable;
    private String imageUrl;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_specific_news_report);

        ActionBar actionBar = this.getSupportActionBar();
        if (actionBar != null) 
            actionBar.setDisplayHomeAsUpEnabled(true);
        
        intent = getIntent();
        LoadImageAsyncTask task = new LoadImageAsyncTask();
        AsyncTask < String, Void, Drawable > d = task.execute();

        try 
            drawable = d.get();
         catch (ExecutionException e) 
            e.printStackTrace();
         catch (InterruptedException e) 
            e.printStackTrace();
        
        imageUrl = getIntent().getStringExtra("image");
        Log.e("drawable specific", "" + drawable);
        drawable = new LoadImageAsyncTask().getImage();
        contentTextView = (TextView) findViewById(R.id.specific_news_report_content_text_view);
        titleTextView = (TextView) findViewById(R.id.specific_news_report_title_text_view);
        imageView = (ImageView) findViewById(R.id.specific_news_report_image_view);
        contentTextView.setText(intent.getStringExtra("content"));
        titleTextView.setText(intent.getStringExtra("title"));
        try 
            if (drawable != null) 
                imageView.setImageDrawable(drawable);
             else 
                Log.e("returned drawable", "null");
            
         catch (Exception ex) 
            Log.e("enna exception", "" + ex);
        

    
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) 
        int id = item.getItemId();
        if (id == android.R.id.home) 
            Intent intent = NavUtils.getParentActivityIntent(this);
            NavUtils.navigateUpTo(this, intent);
        
        return super.onOptionsItemSelected(item);
    



堆栈跟踪

2019-07-29 13:57:20.172 24098-24098/com.example.newsreport E/SQLiteLog: (1) near "null": syntax error
2019-07-29 13:57:20.177 24098-24098/com.example.newsreport E/SQLiteDatabase: Error inserting channel=News18.com null=https://images.news18.com/ibnlive/uploads/2019/07/yediyurappa-1.jpg image=https://images.news18.com/ibnlive/uploads/2019/07/yediyurappa-1.jpg title=Karnataka Assembly Trust Vote LIVE: Yediyurappa Wins Floor Test, Speaker Ramesh Kumar Resigns - News18 content=Eleven Congress MLAs and three JDS lawmakers faced the axe from the Speaker in addition to the three disqualified earlier, bringing down the majority mark to 104, one less than the current strength of 105 of the BJP, which also enjoys the support of an Indepe… [+3489 chars]
    android.database.sqlite.SQLiteException: near "null": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO entry(channel,null,image,title,content) VALUES (?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1562)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
        at com.example.newsreport.NewsFeedProvider.insert(NewsFeedProvider.java:78)
        at android.content.ContentProvider$Transport.insert(ContentProvider.java:265)
        at android.content.ContentResolver.insert(ContentResolver.java:1587)
        at com.example.newsreport.NewsReportActivity.onLoadFinished(NewsReportActivity.java:96)
        at com.example.newsreport.NewsReportActivity.onLoadFinished(NewsReportActivity.java:29)
        at androidx.loader.app.LoaderManagerImpl$LoaderObserver.onChanged(LoaderManagerImpl.java:250)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:113)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:131)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:289)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
        at androidx.loader.app.LoaderManagerImpl$LoaderInfo.setValue(LoaderManagerImpl.java:189)
        at androidx.loader.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManagerImpl.java:174)
        at androidx.loader.content.Loader.deliverResult(Loader.java:132)
        at androidx.loader.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:258)
        at androidx.loader.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:83)
        at androidx.loader.content.ModernAsyncTask.finish(ModernAsyncTask.java:490)
        at androidx.loader.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:507)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-07-29 13:57:20.218 24098-24098/com.example.newsreport E/SQLiteLog: (1) near "null": syntax error
2019-07-29 13:57:20.220 24098-24098/com.example.newsreport E/SQLiteDatabase: Error inserting channel=News18.com null=https://images.news18.com/ibnlive/uploads/2019/07/yediyurappa-1.jpg image=https://images.news18.com/ibnlive/uploads/2019/07/yediyurappa-1.jpg title=Karnataka Assembly Trust Vote LIVE: Yediyurappa Wins Floor Test, Speaker Ramesh Kumar Resigns - News18 content=Eleven Congress MLAs and three JDS lawmakers faced the axe from the Speaker in addition to the three disqualified earlier, bringing down the majority mark to 104, one less than the current strength of 105 of the BJP, which also enjoys the support of an Indepe… [+3489 chars]
    android.database.sqlite.SQLiteException: near "null": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO entry(channel,null,image,title,content) VALUES (?,?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1562)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
        at com.example.newsreport.NewsReportActivity.onLoadFinished(NewsReportActivity.java:102)
        at com.example.newsreport.NewsReportActivity.onLoadFinished(NewsReportActivity.java:29)
        at androidx.loader.app.LoaderManagerImpl$LoaderObserver.onChanged(LoaderManagerImpl.java:250)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:113)

我需要使用从 loadimageasynctask 返回的 drawable 我需要将所有数据存储在数据库以及内容提供者中

【问题讨论】:

请edit您的帖子并包含完整的堆栈跟踪(格式为代码);还指出代码中的哪个语句引发了异常。 @Jim Garrison 先生,我在 NewRowId = db.insert(FeedReaderContract.FeedEntry.TABLE_NAME,null , values) 行的 NewsReportActivity 中遇到异常; 【参考方案1】:

表中有 4 列和主键。 当您插入新行时,您为 ContentValues 对象提供值,但您为每列执行两次:

values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, newsReport.getTitle());
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CONTENT, newsReport.getContent());
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CHANNEL, newsReport.getNewsChannel());
values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_IMAGE, newsReport.getPic());
values.put(NewsFeedProvider.newsTitle, newsReport.getTitle());
values.put(NewsFeedProvider.newsContent, newsReport.getContent());
values.put(NewsFeedProvider.newsChannel, newsReport.getNewsChannel());
values.put(NewsFeedProvider.newsImage, newsReport.getPic()); 

为什么? 我怀疑其中之一:

NewsFeedProvider.newsTitle
NewsFeedProvider.newsContent
NewsFeedProvider.newsChannel
NewsFeedProvider.newsImage

返回null。 所以删除这些行:

values.put(NewsFeedProvider.newsTitle, newsReport.getTitle());
values.put(NewsFeedProvider.newsContent, newsReport.getContent());
values.put(NewsFeedProvider.newsChannel, newsReport.getNewsChannel());
values.put(NewsFeedProvider.newsImage, newsReport.getPic());

它们不是必需的。 列的值由前 4 行设置。

【讨论】:

谢谢....删除这四行后,我的代码工作得更好..但我还有一个查询...我需要在从loadImageAsynctask返回的specificActivity中使用drawable。我已经尝试过了,但是在 specificActivity 中获得了 null 值...请帮我解决这个问题 这与这个问题无关。发布另一个问题并解释问题所在并包含所有需要的代码。

以上是关于如何在android java中修复sqliteException的主要内容,如果未能解决你的问题,请参考以下文章

如何使用java将相机点击图片存储在Android sqlite数据库中

如何在 Android 中使用 SQLite?

如何在android java中修复sqliteException

如何在android中创建SQLite数据库

首次开发android应用程序时如何在java中修复“R.menu.main”?

Android,删除列表和sqlite java中的项目