关于SQlite插入数据不显示的问题
packagesqliteshuju;importandroid.content.Context;importandroid.database.sqlite.SQLite...
package sqliteshuju;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class sqlitetext extends SQLiteOpenHelper{
private static final int VERSION =1;
public sqlitetext(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
public sqlitetext(Context context, String name){
this(context,name, VERSION);
}
public sqlitetext(Context context,String name,int version){
this(context,name,null,VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("Create table user(id int,name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub platform-
System.out.println("ssss");
}
}
-----------------------------------------
public class Youxi6Activity extends Activity {
private Button chuangjian=null;
private Button charu=null;
private TextView text=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chuangjian=(Button)findViewById(R.id.chuangjian);
charu=(Button)findViewById(R.id.charu);
text=(TextView)findViewById(R.id.ceshi);
chuangjian.setOnClickListener(new chuangjianshujuku());
charu.setOnClickListener(new charushuju());
}
class chuangjianshujuku implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
sqlitetext st=new sqlitetext(Youxi6Activity.this,"sqlshujuku");
SQLiteDatabase db=st.getReadableDatabase();
}
}
class charushuju implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
ContentValues cv=new ContentValues();
cv.put("id", 1);
cv.put("name", "yuandaw");
sqlitetext st=new sqlitetext(Youxi6Activity.this,"sqlshujuku");
SQLiteDatabase db=st.getWritableDatabase();
db.insert("user", null, cv);
text.setText("yuandaw");
}
}
}
在cmd中select * from user 后 什么也不显示 展开
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class sqlitetext extends SQLiteOpenHelper{
private static final int VERSION =1;
public sqlitetext(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
public sqlitetext(Context context, String name){
this(context,name, VERSION);
}
public sqlitetext(Context context,String name,int version){
this(context,name,null,VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("Create table user(id int,name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub platform-
System.out.println("ssss");
}
}
-----------------------------------------
public class Youxi6Activity extends Activity {
private Button chuangjian=null;
private Button charu=null;
private TextView text=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chuangjian=(Button)findViewById(R.id.chuangjian);
charu=(Button)findViewById(R.id.charu);
text=(TextView)findViewById(R.id.ceshi);
chuangjian.setOnClickListener(new chuangjianshujuku());
charu.setOnClickListener(new charushuju());
}
class chuangjianshujuku implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
sqlitetext st=new sqlitetext(Youxi6Activity.this,"sqlshujuku");
SQLiteDatabase db=st.getReadableDatabase();
}
}
class charushuju implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
ContentValues cv=new ContentValues();
cv.put("id", 1);
cv.put("name", "yuandaw");
sqlitetext st=new sqlitetext(Youxi6Activity.this,"sqlshujuku");
SQLiteDatabase db=st.getWritableDatabase();
db.insert("user", null, cv);
text.setText("yuandaw");
}
}
}
在cmd中select * from user 后 什么也不显示 展开
1个回答
展开全部
首先我说下,我用了你的代码。是可以运行的,而且我也查看了数据库user表内容,成功插入了。首先我有几点不明白:
1.为什么你要单独创建数据库? SQLiteDatabase db=st.getWritableDatabase(); 数据库不存在的话会自动创建。创建的是多余的代码。
2.建议在用完数据库的时候关闭数据库。db.close();
不知道是不是你查询的方法有些问题。你再试试,或者你直接点击“插入数据的按钮”
附上查看数据库命令:
adb shell
sqlite3 data/data/sqliteshuju/databases/sqlshujuku
.table
select * from user ;
1.为什么你要单独创建数据库? SQLiteDatabase db=st.getWritableDatabase(); 数据库不存在的话会自动创建。创建的是多余的代码。
2.建议在用完数据库的时候关闭数据库。db.close();
不知道是不是你查询的方法有些问题。你再试试,或者你直接点击“插入数据的按钮”
附上查看数据库命令:
adb shell
sqlite3 data/data/sqliteshuju/databases/sqlshujuku
.table
select * from user ;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询