Dando erro eclipse...

25/11/2015

1

Assistindo esse vídeo aula:
http://www.devmedia.com.br/executando-testes-no-banco-e-tabelas-curso-de-android-criando-uma-loja-virtual-aula-40/29949#comentarios

11-25 13:45:23.966: E/SQLiteOpenHelper(538): Couldn't open db_supermercado for writing (will try read-only):
11-25 13:45:23.966: E/SQLiteOpenHelper(538): android.database.sqlite.SQLiteException: near "USUARIO": syntax error: , while compiling: CREATE TABLE TB USUARIO (ID_USUARIO INTEGER PRIMARY KEY AUTOINCREMENT,LOGIN TEXT NOT NULL,SENHA TEXT NOT NULL)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:134)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at br.supermercado.sqlite.LoginOpenHelper.onCreate(LoginOpenHelper.java:32)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:231)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at br.supermercado.sqlite.LoginOpenHelper.validarLogin(LoginOpenHelper.java:54)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at br.supermercado.bo.LoginBO.validarLogin(LoginBO.java:24)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at br.supermercado.LoginActivity$LoadingAsync.doInBackground(LoginActivity.java:69)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at br.supermercado.LoginActivity$LoadingAsync.doInBackground(LoginActivity.java:1)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-25 13:45:23.966: E/SQLiteOpenHelper(538): 	at java.lang.Thread.run(Thread.java:856)
11-25 13:45:23.996: W/dalvikvm(538): threadid=11: thread exiting with uncaught exception (group=0x409961f8)
11-25 13:45:24.066: E/AndroidRuntime(538): FATAL EXCEPTION: AsyncTask #1
11-25 13:45:24.066: E/AndroidRuntime(538): java.lang.RuntimeException: An error occured while executing doInBackground()
11-25 13:45:24.066: E/AndroidRuntime(538): 	at android.os.AsyncTask$3.done(AsyncTask.java:278)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at java.lang.Thread.run(Thread.java:856)
11-25 13:45:24.066: E/AndroidRuntime(538): Caused by: android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 0 to 1: /data/data/br.supermecado/databases/db_supermercado
11-25 13:45:24.066: E/AndroidRuntime(538): 	at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:244)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at br.supermercado.sqlite.LoginOpenHelper.validarLogin(LoginOpenHelper.java:54)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at br.supermercado.bo.LoginBO.validarLogin(LoginBO.java:24)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at br.supermercado.LoginActivity$LoadingAsync.doInBackground(LoginActivity.java:69)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at br.supermercado.LoginActivity$LoadingAsync.doInBackground(LoginActivity.java:1)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-25 13:45:24.066: E/AndroidRuntime(538): 	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-25 13:45:24.066: E/AndroidRuntime(538): 	... 5 more
11-25 13:45:24.387: W/IInputConnectionWrapper(538): showStatusIcon on inactive InputConnection
11-25 13:45:25.626: E/WindowManager(538): Activity br.supermercado.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@413ac2b8 that was originally added here
11-25 13:45:25.626: E/WindowManager(538): android.view.WindowLeaked: Activity br.supermercado.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@413ac2b8 that was originally added here
11-25 13:45:25.626: E/WindowManager(538): 	at android.view.ViewRootImpl.<init>(ViewRootImpl.java:343)
11-25 13:45:25.626: E/WindowManager(538): 	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:245)
11-25 13:45:25.626: E/WindowManager(538): 	at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
11-25 13:45:25.626: E/WindowManager(538): 	at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
11-25 13:45:25.626: E/WindowManager(538): 	at android.view.Window$LocalWindowManager.addView(Window.java:537)
11-25 13:45:25.626: E/WindowManager(538): 	at android.app.Dialog.show(Dialog.java:274)
11-25 13:45:25.626: E/WindowManager(538): 	at br.supermercado.LoginActivity$LoadingAsync.onPreExecute(LoginActivity.java:59)
11-25 13:45:25.626: E/WindowManager(538): 	at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
11-25 13:45:25.626: E/WindowManager(538): 	at android.os.AsyncTask.execute(AsyncTask.java:511)
11-25 13:45:25.626: E/WindowManager(538): 	at br.supermercado.LoginActivity.logar(LoginActivity.java:42)
11-25 13:45:25.626: E/WindowManager(538): 	at java.lang.reflect.Method.invokeNative(Native Method)
11-25 13:45:25.626: E/WindowManager(538): 	at java.lang.reflect.Method.invoke(Method.java:511)
11-25 13:45:25.626: E/WindowManager(538): 	at android.view.View$1.onClick(View.java:3034)
11-25 13:45:25.626: E/WindowManager(538): 	at android.view.View.performClick(View.java:3480)
11-25 13:45:25.626: E/WindowManager(538): 	at android.view.View$PerformClick.run(View.java:13983)
11-25 13:45:25.626: E/WindowManager(538): 	at android.os.Handler.handleCallback(Handler.java:605)
11-25 13:45:25.626: E/WindowManager(538): 	at android.os.Handler.dispatchMessage(Handler.java:92)
11-25 13:45:25.626: E/WindowManager(538): 	at android.os.Looper.loop(Looper.java:137)
11-25 13:45:25.626: E/WindowManager(538): 	at android.app.ActivityThread.main(ActivityThread.java:4340)
11-25 13:45:25.626: E/WindowManager(538): 	at java.lang.reflect.Method.invokeNative(Native Method)
11-25 13:45:25.626: E/WindowManager(538): 	at java.lang.reflect.Method.invoke(Method.java:511)
11-25 13:45:25.626: E/WindowManager(538): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-25 13:45:25.626: E/WindowManager(538): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-25 13:45:25.626: E/WindowManager(538): 	at dalvik.system.NativeStart.main(Native Metho
Responder

Posts

25/11/2015

Usuario

Jorge,
Verifique o nome da sua tabela no código.. Você colocou TB USUARIO com espaço?
Responder

25/11/2015

Jorge Conforte

package br.supermercado.sqlite;

import java.util.Locale;
import java.util.ResourceBundle;

import br.supermercado.comum.Constantes;

import android.content.ContentValues;
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 LoginOpenHelper extends SQLiteOpenHelper{
	
	private static ResourceBundle config = ResourceBundle.getBundle(Constantes.DB_CONFIG_PROPS, Locale.getDefault());

	public LoginOpenHelper(Context context) {
		super(context, config.getString(Constantes.DB_CONFIG_NOME), null, Integer.parseInt(config.getString(Constantes.DB_CONFIG_VERSAO)));
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		StringBuilder sql = new StringBuilder();
		sql.append("CREATE TABLE TB USUARIO (");
		sql.append("ID_USUARIO INTEGER PRIMARY KEY AUTOINCREMENT,");
		sql.append("LOGIN TEXT NOT NULL,");
		sql.append("SENHA TEXT NOT NULL)");
		
		db.execSQL(sql.toString());
		mockPopulaUsuarios(db);
		db.close();
	}
	
	private void mockPopulaUsuarios(SQLiteDatabase db) {
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO TB_USUARIO(LOGIN, SENHA) VALUES('Diogo', '123')");
		db.execSQL(sql.toString());
		
		ContentValues values = new ContentValues();
		values.put("LOGIN", "admin");
		values.put("SENHA", "admin");
		db.insert("TB_USUARIO", null, values);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	
	public boolean validarLogin(String usuario, String senha) {
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.query("TB_USUARIO", null, "LOGIN = ? AND SENHA = ?", new String[]{usuario, senha}, null, null, null);
		if (cursor.moveToFirst()) {
			return true;
		}
		return false;
	}
}
Responder

25/11/2015

Jorge Conforte

Dá pra entender?
Responder

25/11/2015

Usuario

É o nome da sua tabela...
No seu código está com espaço no nome.. Seu código está assim:
sql.append("CREATE TABLE TB USUARIO (");


E acho que você queria que fosse assim:
sql.append("CREATE TABLE TB_USUARIO (");


Está faltando o _ (underline) entre o TB e USUARIO
Responder

25/11/2015

Jorge Conforte

Eu vejo o erro no console ou no logcat?
Responder

25/11/2015

Usuario

Se não me engano aparece tanto no console como no logcat... eu vejo pelo logcat!
Mas seu erro é devido ao nome da tabela... quando você escreveu
CREATE TABLE TB USUARIO (

ficou com espaço... basta mudar a sua linha 27 para
sql.append("CREATE TABLE TB_USUARIO (");


É esse o problema que está no seu SQL! Troque e faça um teste novamente!

Você já corrigiu isso?
Responder

25/11/2015

Jorge Conforte

Opha deu certo...
Responder
×
+1 DevUP
Acesso diário, +1 DevUP
Parabéns, você está investindo na sua carreira