我是Android新手。尝试将数据保存到Sqlite数据库。我正在使用“选项卡式活动”,所以我的项目中有片段。我不明白如何与片段通信我的数据库。我想使用etAdi(编辑文本ID)作为用户名,并使用bKaydet(按钮保存)进行保存。
我试图把
final Veritabani veritabani =新Veritabani(getActivity());
此代码(Veritabani的意思是数据库),但是当我使用veritabani.open()和veritabani.close()时,应用中出现错误。试图将此数据库代码写入SubPage01.java(Fragment)。请帮忙..
我的数据库类:Veritabani.java
Veritabani.java
package com.guly.projecttabmenu;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import layout.SubPage01;
public class Veritabani{
public static final String ROW_ID = "id";
public static final String ROW_NAME = "adi";
public static final String ROW_SURNAME = "soyadi";
private static final String DATABASE_NAME = "veritabani";
private static final int DATABASE_VERSION = 1;
private static final String CUSTOMERINFORMATION_TABLE = "customerinformation"; //customerinformation = kisiler
private Context ourContext;
private DbHelper ourHelper;
private SQLiteDatabase ourDatabase;
public Veritabani(Context context)
{
ourContext=context;
}
public class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE" + CUSTOMERINFORMATION_TABLE + "(" + ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ROW_NAME + " TEXT NOT NULL, " + ROW_SURNAME + " TEXT NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + CUSTOMERINFORMATION_TABLE);
onCreate(db);
}
}
public Veritabani open(){
ourHelper=new DbHelper(ourContext);
ourDatabase=ourHelper.getWritableDatabase();
return this;
}
public Veritabani close(){
ourHelper.close();
return this;
}
}
`
MainActivity.java
package com.guly.projecttabmenu;
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onStart() {
super.onStart();
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.guly.projecttabmenu/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.guly.projecttabmenu/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
//Veri tabanı oluşturuyoruz
Veritabani veritabani = new Veritabani(MainActivity.this);
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
SubPage01 tab1 = new SubPage01();
return tab1;
case 1:
subPage02 tab2 = new subPage02();
return tab2;
case 2:
subPage03 tab3 = new subPage03();
return tab3;
default:
return null;
}
}
public int getCount() {
return 3;}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Müşteri Kaydet";
case 1:
return "Müşteri Listele";
case 2:
return "Rapor Al";
}
return null;
}
}
}
fragment_sub_page01.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context="layout.SubPage01">
<!-- TODO: Update blank fragment layout -->
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/etAdi"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="@string/Musteri_Adi" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/etSoyadi"
android:layout_below="@+id/etAdi"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="@string/Musteri_Soyadi" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kaydet"
android:id="@+id/bKaydet"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="75dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Listele"
android:id="@+id/bListele"
android:layout_alignTop="@+id/bKaydet"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="30dp"
android:text="TextView"
android:id="@+id/tvSonuc"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/etSoyadi"
android:layout_alignRight="@+id/bListele"
android:layout_alignEnd="@+id/bListele" />
</RelativeLayout>
SubPage01.java
public class SubPage01 extends Fragment implements OnClickListener {
EditText Soyadi,Adi;
Button btnKaydet, bListele;
TextView sonuc;
int counter=0;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sub_page01, container, false);
/** String[] dizi={"gulay","enes","fadime","haydar"};
ListView listView = (ListView) rootView.findViewById(R.id.lvListe);
ArrayAdapter<String> listViewAdapter= new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,dizi);
listView.setAdapter(listViewAdapter);
*/
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
btnKaydet= (Button) getActivity().findViewById(R.id.bKaydet);
btnKaydet.setOnClickListener(this);
sonuc=(TextView) getActivity().findViewById(R.id.tvSonuc);
Soyadi=(EditText) getActivity().findViewById(R.id.etSoyadi);
Adi=(EditText) getActivity().findViewById(R.id.etAdi);
}
@Override
public void onClick(View v) {
/// final Veritabani veritabani=new Veritabani(getActivity()); ??
String ad=Adi.getText().toString();
String soyad=Soyadi.getText().toString();
/// veritabani.open(); ??
sonuc.setText(ad+" eklendi");
/// veritabani.close(); ??
}
}
这是我的日志
12-13 07:39:39.206 29293-29293/? W/System: ClassLoader referenced unknown path: /data/app/com.guly.projecttabmenu-2/lib/x86
12-13 07:39:39.511 29293-29309/? W/EGL_emulation: eglSurfaceAttrib not implemented
12-13 07:39:39.511 29293-29309/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe9921960, error=EGL_SUCCESS
12-13 07:39:45.831 29293-29309/com.guly.projecttabmenu W/EGL_emulation: eglSurfaceAttrib not implemented
12-13 07:39:45.831 29293-29309/com.guly.projecttabmenu W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe8ebdb80, error=EGL_SUCCESS
12-13 07:39:47.775 29293-29309/com.guly.projecttabmenu E/Surface: getSlotFromBufferLocked: unknown buffer: 0xeb0ed870
12-13 07:39:53.740 29293-29309/com.guly.projecttabmenu W/EGL_emulation: eglSurfaceAttrib not implemented
12-13 07:39:53.741 29293-29309/com.guly.projecttabmenu W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe9921c80, error=EGL_SUCCESS
12-13 07:39:54.147 29293-29309/com.guly.projecttabmenu E/Surface: getSlotFromBufferLocked: unknown buffer: 0xeb0ed800
12-13 07:39:59.443 29293-29293/com.guly.projecttabmenu E/SQLiteLog: (1) near "TABLEcustomerinformation": syntax error
12-13 07:39:59.443 29293-29293/com.guly.projecttabmenu E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.guly.projecttabmenu, PID: 29293
android.database.sqlite.SQLiteException: near "TABLEcustomerinformation": syntax error (code 1): , while compiling: CREATE TABLEcustomerinformation(id INTEGER PRIMARY KEY AUTOINCREMENT, adi TEXT NOT NULL, soyadi TEXT NOT NULL)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
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.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.guly.projecttabmenu.Veritabani$DbHelper.onCreate(Veritabani.java:42)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.guly.projecttabmenu.Veritabani.open(Veritabani.java:57)
at layout.SubPage01.onClick(SubPage01.java:74)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
最佳答案
尝试,
您在CREATE TABLE语句后错过了空间。
db.execSQL("CREATE TABLE " + CUSTOMERINFORMATION_TABLE + "(" + ROW_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ROW_NAME + " TEXT NOT NULL, " + ROW_SURNAME + " TEXT NOT NULL)");