1

Estou obtendo um erro no meu código e não consigo resolver, será que poderiam me dar uma ajudinha?

Estou obtendo o erro "E/SQLiteLog: (1) no such column":

09-27 04:04:14.798 14342-14342/com.pineapple.pineapple_mobi1 E/SQLiteLog: (1) no such column: Aline 09-27 04:04:14.801 14342-14342/com.pineapple.pineapple_mobi1 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.pineapple.pineapple_mobi1, PID: 14342 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pineapple.pineapple_mobi1/com.pineapple.pineapple_mobi1.MainActivity}: android.database.sqlite.SQLiteException: no such column: Aline (code 1): , while compiling: SELECT _id, noteText, Aline FROM notes ORDER BY Aline DESC


NotesProvider.java:

package com.pineapple.pineapple_mobi1;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;


/**
 * Created by aline on 25/09/17.
 */

public class NotesProvider extends ContentProvider {


    private static final String AUTHORITY = "com.pineapple.pineapple_mobi1.notesprovider";
    private static final String BASE_PATH = "notes";
    public static final Uri CONTENT_URI =
            Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);


    private static final int NOTES = 1;
    private static final int NOTES_ID = 2;


    private static final UriMatcher uriMatcher=
            new UriMatcher(UriMatcher.NO_MATCH);
    static {
        uriMatcher.addURI(AUTHORITY, BASE_PATH, NOTES);
        uriMatcher.addURI(AUTHORITY, BASE_PATH + "/#", NOTES_ID);
    }


    private SQLiteDatabase database;

    @Override
    public boolean onCreate() {
        DBOpenHelper helper = new DBOpenHelper(getContext());
        database = helper.getWritableDatabase();
        return true;
    }


    @Nullable
    @Override

    public Cursor query(Uri uri, String[] strings, String s, String[] strings1, String s1) {

    //CRUD operations
    return database.query(DBOpenHelper.TABLE_NOTES, DBOpenHelper.ALL_COLUMNS, s, null,null,null, DBOpenHelper.NOTE_CREATED + " DESC");
    //FIM CRUD operations


    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {

        long id = database.insert(DBOpenHelper.TABLE_NOTES, null, contentValues);
        return Uri.parse(BASE_PATH + "/" +id);
    }

    @Override
    public int delete(Uri uri, String s, String[] strings) {

        return database.delete(DBOpenHelper.TABLE_NOTES,s,strings);

    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {

        return database.update(DBOpenHelper.TABLE_NOTES, contentValues, s, strings);

    }
}

DBOpenHelper.Java

package com.pineapple.pineapple_mobi1;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


/**
 * Created by aline on 25/09/17.
 */

public class DBOpenHelper extends SQLiteOpenHelper{



    private static final String DATABASE_NAME = "notes.db";
    private static final int DATABASE_VERSION = 1;
    public static final String TABLE_NOTES= "notes";
    public static final String NOTE_ID = "_id";
    public static final String NOTE_TEXT = "noteText";
    public static final String NOTE_CREATED = "Aline";


//    public static final String[] ALL_COLUMNS = {NOTE_ID, NOTE_TEXT, NOTE_CREATED};
public static final String[] ALL_COLUMNS = {NOTE_ID, NOTE_TEXT, NOTE_CREATED };


    private static final String TABLE_CREATE =
            "CREATE TABLE " + TABLE_NOTES + "("
                    + NOTE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                    + NOTE_TEXT + " TEXT, "
                    + NOTE_CREATED + " TEXT default CURRENT_TIMESTAMP" + ")";


    public DBOpenHelper (Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTES);
        onCreate(sqLiteDatabase);
    }
}

MainActivity.Java

package com.pineapple.pineapple_mobi1;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        insertNote("New note");



        Cursor cursor = getContentResolver().query(NotesProvider.CONTENT_URI, DBOpenHelper.ALL_COLUMNS, null, null, null, null);
        String[] from = {DBOpenHelper.NOTE_TEXT};
        int[] to = {android.R.id.text1};
        CursorAdapter cursorAdapter =new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to, 0);

        ListView list = (ListView) findViewById(android.R.id.list);
        list.setAdapter(cursorAdapter);


    }

    private void insertNote(String noteText) {
        ContentValues values = new ContentValues();
        values.put(DBOpenHelper.NOTE_TEXT, noteText);
        Uri noteUri = getContentResolver().insert(NotesProvider.CONTENT_URI, values);

        Log.d("MainActivity", "Inserted note " + noteUri.getLastPathSegment());
    }
}

cat log:

09-27 04:04:14.798 14342-14342/com.pineapple.pineapple_mobi1 E/SQLiteLog: (1) no such column: Aline
09-27 04:04:14.801 14342-14342/com.pineapple.pineapple_mobi1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.pineapple.pineapple_mobi1, PID: 14342                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pineapple.pineapple_mobi1/com.pineapple.pineapple_mobi1.MainActivity}: android.database.sqlite.SQLiteException: no such column: Aline (code 1): , while compiling: SELECT _id, noteText, Aline FROM notes ORDER BY Aline DESC                                                                                 
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)                                                                                 
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)                                                                                 
   at android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                                 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)                                                                                 
   at android.os.Handler.dispatchMessage(Handler.java:110)                                                                                 
   at android.os.Looper.loop(Looper.java:208)                                                                                 
   at android.app.ActivityThread.main(ActivityThread.java:6267)                                                                                 
   at java.lang.reflect.Method.invoke(Native Method)                                                                                 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)                                                                                 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)                                                                                 Caused by: android.database.sqlite.SQLiteException: no such column: Aline (code 1): , while compiling: SELECT _id, noteText, Aline FROM notes ORDER BY Aline DESC                                                                                 
   at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)                                                                                 
   at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:905)                                                                                 
   at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:516)                                                                                 
   at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)                                                                                 
   at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)                                                                                 
   at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)                                                                                 
   at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)                                                                                 
   at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1348)                                                                                 
   at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1195)                                                                                 
   at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1066)                                                                                 
   at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1272)                                                                                 
   at com.pineapple.pineapple_mobi1.NotesProvider.query(NotesProvider.java:54)                                                                                 
   at android.content.ContentProvider.query(ContentProvider.java:1038)                                                                                 
   at android.content.ContentProvider$Transport.query(ContentProvider.java:247)                                                                                 
   at android.content.ContentResolver.query(ContentResolver.java:602)                                                                                 
   at com.pineapple.pineapple_mobi1.MainActivity.onCreate(MainActivity.java:26)                                                                                 
   at android.app.Activity.performCreate(Activity.java:6701)                                                                                 
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                                                                                 
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)                                                                                 
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2789)                                                                                  
   at android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                                  
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)                                                                                  
   at android.os.Handler.dispatchMessage(Handler.java:110)                                                                                  
   at android.os.Looper.loop(Looper.java:208)                                                                                  
   at android.app.ActivityThread.main(ActivityThread.java:6267)                                                                                  
   at java.lang.reflect.Method.invoke(Native Method)                                                                                  
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)                                                                                  
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
1
  • Essa coluna existe desde o momento em que a tabela foi criada ou foi adicionada, ao DBOpenHelper, depois?
    – ramaral
    Commented 27/09/2017 às 13:31

1 Resposta 1

0

Aline,

Você tem duas formas de resolver esse problema:

1º Você deve alterar a versão do seu banco local, pra que ele enxergue as colunas novas que você adicionou, exemplo:

private static final int DATABASE_VERSION = 2;

2º Antes de rodar a sua aplicação novamente pelo AndroidStudio, desinstale o aplicativo do device, pois quando for instalado novamente e não somente substituído, já vai ser adicionado o novo banco local com todas as colunas.

0

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .