我写个小小的sqlite程序,一跑就死了,咋回事呢~~~

Kross 发布于 2013/04/25 20:58
阅读 1K+
收藏 0
package com.example.testsqlite;
//各种import

public class MainActivity extends Activity {
	
	private Button createDatabaseButton = null;
	private Button updateDatabaseButton = null;
	private Button insertDataButton = null;
	private Button deleteDataButton = null;
	private Button updateDataButton = null;
	private Button queryDataButton = null;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		createDatabaseButton = (Button)findViewById(R.id.create_database);
		updateDatabaseButton = (Button)findViewById(R.id.update_database);
		insertDataButton = (Button)findViewById(R.id.insert_data);
		deleteDataButton = (Button)findViewById(R.id.delete_data);
		updateDataButton = (Button)findViewById(R.id.update_data);
		queryDataButton = (Button)findViewById(R.id.query_data);
		
		createDatabaseButton.setOnClickListener(new CreateDatabaseListener());
		updateDatabaseButton.setOnClickListener(new UpdateDatabaseListener());
		insertDataButton.setOnClickListener(new InsertDataListener());
		deleteDataButton.setOnClickListener(new DeleteDataListener());
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	/**
	 * class CreateDatabaseListener
	 * 内部类,创建数据库按钮的监听器
	 */
	class CreateDatabaseListener implements OnClickListener{
		public void onClick(View v) {
			SQLiteHelper sqliteHelper = new SQLiteHelper(MainActivity.this, "test", 1);
			SQLiteDatabase db = sqliteHelper.getReadableDatabase();
		}
	}
	
	/**
	 * class UpdateDatabaseListener
	 * 内部类,更新数据库按钮的监听器
	 */
	class UpdateDatabaseListener implements OnClickListener{
		public void onClick(View v) {
			SQLiteHelper sqliteHelper = new SQLiteHelper(MainActivity.this, "test", 2);
			SQLiteDatabase db = sqliteHelper.getReadableDatabase();
		}
	}
	
	/**
	 * class InsertDataListener
	 * 内部类,插入数据按钮的监听器
	 */
	class InsertDataListener implements OnClickListener{
		public void onClick(View v) {
			String sql = "INSERT INTO `user` (`id`, `username`) VALUES ('1', 'kross')";
			SQLiteHelper sqLiteHelper = new SQLiteHelper(MainActivity.this, "test", 1);
			SQLiteDatabase db = sqLiteHelper.getWritableDatabase();
			db.execSQL(sql);
		}
	}
	
	class DeleteDataListener implements OnClickListener{
		public void onClick(View v) {
			Toast.makeText(MainActivity.this, "deleteDataButton", Toast.LENGTH_SHORT).show();
			
		}
		
	}

}
接下来是SQLiteHelper的代码

package com.example.testsqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLiteHelper extends SQLiteOpenHelper {

	public SQLiteHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}
	
	public SQLiteHelper(Context context, String name, int version) {
		this(context, name, null, version);
	}

	public void onCreate(SQLiteDatabase db) {
		Log.i("msg", "create database");
		db.execSQL("CREATE TABLE user{id int, username varchar(20)}");
		
	}

	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.i("msg", "update database");
	}
}

 
然后我一运行就报错了~ 求大神帮助……
加载中
0
Ryan-瑞恩
Ryan-瑞恩
报错信息是什么啊??你单步调试了吗???
Ryan-瑞恩
Ryan-瑞恩
回复 @Kross : 直接操作不了,就在DDMS那个窗口找,有一个可以导出文件的。
Kross
Kross
回复 @Rayn-瑞恩 : 配置了,我用adb,进入data文件夹,它提示我没有权限,我用的是手机USB调试的~DDMS里面有个看文件夹的,里面有个DATA文件夹,但是我点不开T_T
Ryan-瑞恩
Ryan-瑞恩
回复 @Kross : 可以啊。。。你配置环境变量了吗?
Kross
Kross
回复 @Rayn-瑞恩 : 我还想问下,DDMS可不可以看见sqlite数据库啊?一定要用adb才能看见么~
Ryan-瑞恩
Ryan-瑞恩
回复 @Kross : SQL语句你都忘光了。 是()不是{ }.
下一页
0
魂歌
魂歌
create table user ( 圆括号
Kross
Kross
恩恩,多谢啦~
0
lzsCommunity
lzsCommunity

楼上正解!这里:CREATE TABLE user{id int, username varchar(20)}

E/AndroidRuntime(5334): android.database.sqlite.SQLiteException: unrecognized token: "{" (code 1): , while compiling: CREATE TABLE user{id int, username varchar(20)}讲的很清楚了



Kross
Kross
T.T好吧,我错了,我做web开发一直都用navicat来建表的~~~谢谢你的解答
0
a
andr
异常报的这么明显都么看见? 建议自己多看看  实在没办法再问
Kross
Kross
恩,我会努力的~
返回顶部
顶部